pair + map 函数结合使用

题目链接:codeforces 44A
5
birch yellow
maple red
birch yellow
maple yellow
maple green
4
3
oak yellow
oak yellow
oak yellow
1
题目大意:每行的开始是一个n,表示一下有 n 行的数据,每行有两个字符串;
目的就是判断除去重复的组还剩余多少组;
AC代码一之map+pair:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<string>
 7 #include<cmath>
 8 #include<map>
 9 using namespace std;
10 int main()
11 {
12   int T,n;
13   string s1,s2;
14   map< pair<string,string> ,int >a;
15   while(cin>>n)
16   {   int ans = 0;
17       for(int i=0; i<n; i++)
18       {
19           cin>>s1>>s2;
20           if(a[make_pair(s1,s2)] == 0)
21           {
22               a[make_pair(s1,s2)]=1;
23               ans++;
24           }
25       }
26       printf("%d
",ans);
27   }
28   return 0;
29 }

AC代码二之map + getline

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<string>
 7 #include<cmath>
 8 #include<map>
 9 using namespace std;
10 map<string ,int >a;
11 int main()
12 {
13     int n;
14    string s;
15   while(cin>>n)
16   {  int ans = 0;
17       getchar();
18       for(int i=0;i<n;i++)
19       {
20           getline(cin,s);
21             if(a[s] == 0 )
22             {
23                 a[s]=1;
24                 ans++;
25             }
26       }
27     printf("%d
",ans);
28   }
29   return 0;
30 }
原文地址:https://www.cnblogs.com/lovychen/p/4025641.html