Think:
1知识点:map初级应用
2反思:map数组不要忘记初始化
3题意概述:n个朋友名单,m个已回消息名单,问还需回复的朋友数量
4题意注意:
1>字母串中大小写不敏感,需转化
2>map数组应用时不要忘记初始化
以下为Wrong Answer代码——map数组没有初始化
#include <bits/stdc++.h>
using namespace std;
void tra(char *st);
char st[14];
int main(){
map<string, int> m1;
int n, m;
while(scanf("%d", &n) && n){
scanf("%d", &m);
getchar();
for(int i = 0; i < n; i++){
scanf("%s", st);
tra(st);
m1[st]++;
}
for(int i = 0; i < m; i++){
scanf("%s", st);
tra(st);
if(m1.count(st)){
m1.erase(st);
}
}
int len = m1.size();
printf("%d\n", len);
}
return 0;
}
void tra(char *st){
int len = strlen(st);
for(int i = 0; i < len; i++){
if(st[i] >= 'A' && st[i] <= 'Z'){
st[i] = st[i] - 'A' + 'a';
}
}
}
/***************************************************
User name:
Result: Wrong Answer
Take time: 212ms
Take Memory: 8540KB
Submit time: 2017-07-14 08:13:07
****************************************************/
以下为Wrong Answer代码——字母串中大小写不敏感需转化
#include <bits/stdc++.h>
using namespace std;
void tra(char *st);
char st[14];
int main(){
map<string, int> m1;
int n, m;
while(scanf("%d", &n) && n){
scanf("%d", &m);
getchar();
m1.clear();///初始化—清空所有元素
for(int i = 0; i < n; i++){
scanf("%s", st);
/*tra(st);*/
m1[st]++;
}
for(int i = 0; i < m; i++){
scanf("%s", st);
/*tra(st);*/
if(m1.count(st)){
m1.erase(st);
}
}
int len = m1.size();
printf("%d\n", len);
}
return 0;
}
void tra(char *st){
int len = strlen(st);
for(int i = 0; i < len; i++){
if(st[i] >= 'A' && st[i] <= 'Z'){
st[i] = st[i] - 'A' + 'a';
}
}
}
/***************************************************
User name:
Result: Wrong Answer
Take time: 140ms
Take Memory: 2344KB
Submit time: 2017-07-14 08:21:44
****************************************************/
以下为Accepted代码
#include <bits/stdc++.h>
using namespace std;
void tra(char *st);
char st[14];
int main(){
map<string, int> m1;
int n, m;
while(scanf("%d", &n) && n){
scanf("%d", &m);
getchar();
m1.clear();///初始化—清空所有元素
for(int i = 0; i < n; i++){
scanf("%s", st);
tra(st);
m1[st]++;
}
for(int i = 0; i < m; i++){
scanf("%s", st);
tra(st);
if(m1.count(st)){
m1.erase(st);
}
}
int len = m1.size();
printf("%d\n", len);
}
return 0;
}
void tra(char *st){
int len = strlen(st);
for(int i = 0; i < len; i++){
if(st[i] >= 'A' && st[i] <= 'Z'){
st[i] = st[i] - 'A' + 'a';
}
}
}
/***************************************************
User name:
Result: Accepted
Take time: 160ms
Take Memory: 2340KB
Submit time: 2017-07-14 08:16:30
****************************************************/
以下为Accepted代码
#include <bits/stdc++.h>
using namespace std;
void tra(char *st);
char st[14];
int main(){
map<string, int> m1;
int n, m;
while(scanf("%d", &n) && n){
scanf("%d", &m);
getchar();
m1.clear();///初始化—清空所有元素
for(int i = 0; i < n; i++){
scanf("%s", st);
tra(st);
m1[st]++;
}
for(int i = 0; i < m; i++){
scanf("%s", st);
tra(st);
if(m1.count(st)){
m1.erase(st);
}
}
int len = m1.size();
printf("%d\n", len);
}
return 0;
}
void tra(char *st){
int len = strlen(st);
for(int i = 0; i < len; i++){
st[i] = towlower(st[i]);/*towlower()将大写字母转换为小写字母*/
}
}
/***************************************************
User name:
Result: Accepted
Take time: 164ms
Take Memory: 2340KB
Submit time: 2017-07-14 08:24:22
****************************************************/