map与vector---Email Aliases
2016-05-22 16:12
148 查看
Description
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.comaddresses, 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, addressesACM.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: addressespolycarp+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.
Sample Input
Input
Output
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.comaddresses, 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, addressesACM.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: addressespolycarp+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.
Sample Input
Input
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
Output
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 代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <map> using namespace std; typedef long long LL; const int MAXN = 2e4+5; const double eps = 1e-7; char str[MAXN]; char S[MAXN]; map <string, int> mp; vector<string> vec[MAXN]; int main() { int n; while(cin>>n) { for(int i=0; i<MAXN; i++) vec[i].clear(); mp.clear(); int tmp = 0, ans = 0; for(int i=0; i<n; i++) { cin>>str; strcpy(S,str); int len = strlen(str); for(int ii=0; ii<len; ii++) { if(str[ii]>='A'&&str[ii]<='Z') str[ii] = str[ii]+'a'-'A'; if(str[ii] == '@') tmp = ii; } if(strcmp(str+tmp+1,"bmail.com")==0) { int l = 0;///变化之后的长度 for(int j=0; j<tmp; j++) { if(str[j] == '.') continue; if(str[j] == '+') break; str[l++] = str[j]; } for(int j=tmp; str[j]; j++) str[l++] = str[j]; str[l] = '\0'; } if(mp.find(str) == mp.end())///判断有没有相同 mp[str] = ans++; int tt = mp[str]; vec[tt].push_back(S); } cout<<ans<<endl; for(int i=0; i<ans; i++) { cout<<vec[i].size(); for(int j=0; j<vec[i].size(); j++) cout<<" "<<vec[i][j]; puts(""); } } return 0; }
相关文章推荐
- 人工智能之机器学习常见算法
- 安装CENTOS 6.5 32位(CentOS-6.5-i386)+postfix+dovecot+openwebmail passwd验证的邮件系统
- Uva 507 - Jill Rides Again(最大子数组求和问题)
- 使用aidl工具快速在应用层实现binder进程间通信
- zabbix3.0.2 实现发送email报警的详细过程
- AIX操作系统通过光盘进行备份与恢复
- compress_pair_ref
- 谷歌约束控件(ConstraintLayout)扁平化布局入门
- HDU 5029 Relief grain 树链剖分 好题
- toString() unavailable - no suspended threads
- Airbnb 分享经济步步紧逼:传统酒店老板要和出租车司机同病相怜?
- 【NOIP2013模拟】Rainbow的信号
- Block如何避免self retain
- LeetCode 70. Climbing Stairs(爬楼梯)
- 3881: [Coci2015]Divljak fail树+树链的并
- 人工智能:美女机器人能和男人产生感情吗?
- ArrayList<E>.<init> line: not available
- 【杭电oj】2057 - A + B Again(16进制输入输出)
- POJ 3421 X-factor Chains(数论)(筛法)()
- 2016/5/21 1002. format the book list again~