CodeForces - 589A(vector+map,字符串水题)
Polycarp has quite recently learned about email aliases. Of course, he used to suspect that the case of the letters doesn't matter in email addresses. He also learned that a popular mail server in Berland bmail.com ignores dots (characters '.') and all the part of an address from the first character "plus" ('+') to character "at" ('@') in a login part of email addresses.
Formally, any email address in this problem will look like "login@domain", where:
- a "login" is a non-empty sequence of lowercase and uppercase letters, dots ('.') and pluses ('+'), which starts from a letter;
- a "domain" is a non-empty sequence of lowercase and uppercase letters and dots, at that the dots split the sequences into non-empty words, consisting only from letters (that is, the "domain" starts from a letter, ends with a letter and doesn't contain two or more consecutive dots).
When you compare the addresses, the case of the characters isn't taken into consideration. Besides, when comparing the bmail.com addresses, servers ignore the dots in the login and all characters from the first character "plus" ('+') to character "at" ('@') in login part of an email address.
For example, addresses saratov@example.com and SaratoV@Example.Com correspond to the same account. Similarly, addresses ACM.ICPC.@bmail.com and A.cmIcpc@Bmail.Com also correspond to the same account (the important thing here is that the domains of these addresses are bmail.com). The next example illustrates the use of character '+' in email address aliases: addresses polycarp+contest@BMAIL.COM, Polycarp@bmail.com and polycarp++acm+icpc@Bmail.Com also correspond to the same account on the server bmail.com. However, addresses a@bmail.com.ru and a+b@bmail.com.ru are not equivalent, because '+' is a special character only for bmail.com addresses.
Polycarp has thousands of records in his address book. Until today, he sincerely thought that that's exactly the number of people around the world that he is communicating to. Now he understands that not always distinct records in the address book represent distinct people.
Help Polycarp bring his notes in order by merging equivalent addresses into groups.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 2·104) — the number of email addresses in Polycarp's address book.
The following n lines contain the email addresses, one per line. It is guaranteed that all of them are correct. All the given lines are distinct. The lengths of the addresses are from 3 to 100, inclusive.
Output
Print the number of groups k and then in k lines print the description of every group.
In the i-th line print the number of addresses in the group and all addresses that belong to the i-th group, separated by a space. It is allowed to print the groups and addresses in each group in any order.
Print the email addresses exactly as they were given in the input. Each address should go to exactly one group.
Examples
6
ICPC.@bmail.com
p+con+test@BMAIL.COM
P@bmail.com
a@bmail.com.ru
I.cpc@Bmail.Com
a+b@bmail.com.ru
4
2 ICPC.@bmail.com I.cpc@Bmail.Com
2 p+con+test@BMAIL.COM P@bmail.com
1 a@bmail.com.ru
1 a+b@bmail.com.ru
题意:输入n个邮件服务器,它想把相同的邮件服务器统计出来。
给出的条件:
①忽略字母大小写
②当邮件后缀是@bmail.com时,第一个'+'号到'@'之间的可以忽略,所有'.'可以忽略
③当后缀不是@bmail.com时,只能忽略大小写,不能忽略'+'和'.'
思路:
①因为要查询该邮件和之前的服务器是否相等,所有可以想到要用map,因为map中的find函数用时很低
②相同的服务器要在同一行输出,所有要用到vector
写法:
①先截取后缀,看后缀是否为@bmail.com,如果是的话,运用上面的“条件②”,如果不是,则用“条件③”.....得到change(处理过的邮件服务器)
②用map查询是否有重复的,无重复则添加入map,有重复则在重复处的vector后面加入该邮件服务器的“下标”
③依据题目要求输出
1 #include<stdio.h> 2 #include<string> 3 #include<string.h> 4 #include<vector> 5 #include<iostream> 6 #include<map> 7 using namespace std; 8 struct Node{ 9 string s; 10 string change;//经过处理后的邮件服务器 11 }email[2*10001]; 12 struct{ 13 vector<int> next; 14 }ans[20001]; 15 map<string,int>mp; 16 int sum=0; 17 void compare(int n) 18 { 19 map<string,int>::iterator it; 20 it=mp.find(email[n].change); 21 if(it!=mp.end())//如果不是最后,说明找到了,则有重复 22 { 23 ans[mp[email[n].change]].next.push_back(n);//有重复,则把下标直接放在重复的vector后面 24 return; 25 } 26 else 27 {//如果没重复,则要在map中插入一个新的 28 mp.insert(pair<string,int>(email[n].change,sum)); 29 ans[sum].next.push_back(n);//把新的邮件服务器下标存入vector的第一个位子 30 sum++; 31 return; 32 } 33 } 34 int main() 35 { 36 int n; 37 scanf("%d",&n); 38 for(int i=0;i<n;i++) 39 { 40 string qaq; 41 cin>>email[i].s; 42 int flag=-1; 43 for(int j=email[i].s.length()-1;j>=0;j--)//获取后缀 44 { 45 if(email[i].s[j]<='Z'&&email[i].s[j]>='A') 46 qaq.push_back(email[i].s[j]+32); 47 else 48 qaq.push_back(email[i].s[j]); 49 if(email[i].s[j]=='@') 50 { 51 flag=j;//flag标记@位子 52 break; 53 } 54 } 55 if(qaq=="moc.liamb@")//因为上面是倒着赋值的,所以这里是倒着 56 {//如果满足后缀条件,特殊处理 57 for(int j=0;j<flag;j++)//for到flag(@)就行 58 { 59 if(email[i].s[j]=='+') //遇到第一个‘+’直接跳出循环 60 break; 61 if(email[i].s[j]=='.') 62 continue; 63 if(email[i].s[j]<='Z'&&email[i].s[j]>='A') 64 email[i].change.push_back(email[i].s[j]+32); 65 else 66 email[i].change.push_back(email[i].s[j]); 67 } 68 email[i].change=email[i].change+qaq; 69 } 70 else 71 {//不满足条件处理 72 for(int j=0;j<flag;j++) 73 { 74 if(email[i].s[j]<='Z'&&email[i].s[j]>='A') 75 email[i].change.push_back(email[i].s[j]+32); 76 else 77 email[i].change.push_back(email[i].s[j]); 78 } 79 email[i].change=email[i].change+qaq; 80 } 81 qaq.clear(); 82 compare(i);//比较是否重复 83 } 84 printf("%d\n",sum); 85 for(int i=0;i<sum;i++)//按照要求输出 86 { 87 printf("%d",ans[i].next.size()); 88 for(int j=0;j<ans[i].next.size();j++) 89 { 90 cout<<" "<<email[ans[i].next[j]].s; 91 } 92 cout<<endl; 93 } 94 }