码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForce 589A Email Aliases

时间:2016-08-20 23:26:11      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:

Email Aliases
 

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
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

题意:
  给你一些邮箱,忽略大小写(这是个大前提),有一种特殊邮箱 @bmail.com 这种邮箱有二个特殊地方:
  (1)忽略前面的‘.’这个符号,(2)无视第一个‘+’到‘@’的字符
思路:
  同学用了两重for循环结果,无情超时;
  最好是输入一个处理一个,和前面一样的放进去。
AC代码

技术分享
 1 # include <iostream>
 2 # include <cstring>
 3 # include <cstdio>
 4 # include <vector>
 5 # include <map>
 6 using namespace std;
 7 
 8 const int MAX = 2* 1e4 + 1;
 9  
10 char s[MAX][105];
11 char s2[MAX][105];
12 map <string, int> mp;
13 vector <int> v[MAX];
14 int num = 0;
15  
16 void init(int k)
17 {
18     // 转换大小写
19     for(int i = 1; s[k][i] != \0; i++)
20         if(s[k][i] >= A && s[k][i] <= Z) 
21             s2[k][i] = tolower(s[k][i]);
22         else 
23             s2[k][i]=s[k][i];
24  
25     int p = 1;
26     for(; s[k][p] != @; p++); // 找@
27     
28     //  判段字符串相同吗
29     if(strcmp(s2[k] + p + 1, "bmail.com") == 0)
30     {
31         int flag = 0, cnt = 0;
32         for(int i = 1; s[k][i] != \0; i++)
33         {
34             if((s[k][i] == . || flag) && i < p)
35                 continue;
36             else if(s[k][i] == +) 
37                 flag = 1;
38             else 
39                 s2[k][++cnt] = tolower(s[k][i]);
40         }
41             s2[k][cnt+1] = \0;
42     }
43     if(!mp[s2[k] + 1])
44     {
45         num++;
46         mp[s2[k] + 1] = num;
47         v[num].push_back(k);
48     }
49     else
50         v[mp[s2[k]+1]].push_back(k);
51 }
52  
53 int main()
54 {
55     int n;
56     scanf("%d", &n);
57 
58 
59     for(int i = 1; i <= n; i++)
60     {
61         scanf("%s", s[i] + 1);
62         init(i);
63     }
64     printf("%d\n", num);
65     
66     for(int i = 1; i <= num; i++)
67     {
68         printf("%d ", v[i].size());
69         for(int j = 0; j < v[i].size(); j++)
70             printf("%s ",s[v[i][j]] + 1);
71         
72         printf("\n");
73     }
74     for(int i = 1; i <= num; i++) 
75         v[i].clear();
76    
77    return 0;
78 }
View Code

 


CodeForce 589A Email Aliases

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5791462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!