map与vector---Email Aliases

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 "[email protected]", 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 [email protected] and [email protected] correspond to the same account. Similarly, addresses[email protected] and [email protected] 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[email protected], [email protected] and [email protected] also correspond to the same account on the server bmail.com. However, addresses [email protected] and [email protected] 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[email protected][email protected][email protected][email protected][email protected][email protected]

Output

42 [email protected] [email protected] 2 [email protected] [email protected] 1 [email protected] 1 [email protected] 

代码如下:
#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;
}
时间: 2024-08-29 15:42:15

map与vector---Email Aliases的相关文章

codeforces 589A Email Aliases(map)

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

CodeForce 589A Email Aliases

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

map 和 vector 的erase函数说明

1. map的erase函数使用 这里首先要注意,C++针对map的erase函数有不同的函数原型,这往往是出现问题的关键所在.根据参考文献1: 在C++98中: ? 1 2 3 4 5 (1) void erase (iterator position); (2)size_type erase (const key_type& k); (3)void erase (iterator first, iterator last); 在C++11中: ? 1 2 3 4 5 (1)iterator 

map,vector,pair记录

vector的练习不是很完整,希望能在处理现实问题的时候能够进行补全,看了一些网络上的东西自己试着写了些 #include<iostream> #include<map> #include<vector> #include<string> #include<algorithm> using namespace std; bool cmp(int a,int b) { return a>b; } int main() { vector<

map, set, vector erase的正确使用方法

map, set, vector erase的正确使用方法 在C++中, 如果我们使用STL,那 如何使用 erase() 来删除 map, set, vector 的所有数据?下面将给出正确的使用方法. STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque); 另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时,需要注意一些问题,以避免引起不可预知错误或崩溃. 在使用 list.set 或 map遍历

PAT甲题题解-1022. Digital Library (30)-map映射+vector

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789235.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给出n本书的id.名称.作者.多个关键词.出版社.出版年然后给出m个查询,每个查询包含查询的种类.对应的内容针对每个查询,让你输出所有符合的书的id,从小到大排序,没有的话则输出No Found 首先把每个book的信息都读取处理好,然后按照id排个序

[uva11991]map和vector的入门

给你一个长度为n的数组,进行m次询问,每次询问输入k和v,输出第k次出现v时的下标是多少. n<=1e6 用vector动态开空间,map使数值结合.map每次查找效率大约为logn. map的学习资料https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<ios

CodeForces 589 Email Aliases (匹配,水题)

题意:给定于所有的邮箱,都是由[email protected]这样的形式构成,而且字符都是不区分大小写的. 我们有一种特殊类型的邮箱——@bmail.com, 这种邮箱除了不区分大小写外—— 1,'@'之前的'.',有等同于无 2,'@'之前的第一个'+'之后的字符可以忽略不计 然后其他字符相同的被认定为邮箱相同. 现在给你 n 个邮箱,让你输出每个邮箱出现的次数与所有这个邮箱的原始串. 析:没什么好说的,就是先判断不是@bmail.com,然后再按要求去点,去+和@之间的值,然后一个一个的比

map和vector的迭代器失效问题

当删除一个STL容器(比如map, vector)中的某个元素时, 会引起迭代器失效, 所以, 我们务必提高警惕. 题目: 删除map<int, int>中value为5的倍数的元素. 该题看起来很自然很简单, 实则有迭代器失效的陷阱. 如果对迭代器失效问题一无所知, 则很容易写出如下的错误代码: 1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 typedef map<int, i

C++技术问题总结-第7篇 map、vector、list、deque各自的使用场合

map是关联式的,vector.list.deque是序列式的. map:底层机制RB-tree(红黑树),元素自动排序,键值对. vector:操作方式与array相似,动态空间增长.是连续性空间,支持随机访问. 优点: ? 内存动态增长,不需要指定内存大小. ? 支持随机访问,即支持[]和vector.at(). 缺点: ? 在内部进行插入删除操作效率低. ? 只能在vector的最后进行push和pop. list:对空间的运用有绝对的精准,一点也不浪费.删除插入简单,但不支持随机访问.双