题意
给出了一堆电子邮件地址,要你按照一定的规则分类,然后一类一类地输出。
思路
模拟。
只要正确地理解了题意基本不会有问题,在实现方面,可以考虑使用map的迭代器轻松地完成分类操作,令map的key为邮件地址的Hash(其实就是按照题目规则把等价但不同的邮件地址处理成同一个字符串),value为set<string>用来当作不同形态的邮件地址的容器。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <stack> #include <cmath> #include <map> #include <complex> using namespace std; map<string,vector<string> > table; string convert(string &str){ string name; string domain; int dom; for(dom = 0; dom < str.size(); dom++){ if(str[dom] == '@'){ break; } } for(int i = dom;i < str.size(); i++){ if(isalpha(str[i])){ domain += tolower(str[i]); }else{ domain += str[i]; } } if(domain == "@bmail.com"){ for(int i = 0; i < dom; i++){ if(str[i] == '+'){ break; } if(str[i] == '.'){ continue; } name += tolower(str[i]); } }else{ for(int i = 0; i < dom; i++){ if(isalpha(str[i])){ name += tolower(str[i]); }else{ name += str[i]; } } } return name+domain; } int main(){ int n; cin >> n; table.clear(); for(int i = 1; i <= n; i++){ string str; cin >> str; string equ = convert(str); table[equ].push_back(str); } cout << table.size() << endl; for(map<string,vector<string> >::iterator it = table.begin(); it != table.end(); it++){ cout << (*it).second.size() << ' '; for(int i = 0;i < (*it).second.size(); i++){ cout << (*it).second[i] << ' '; } cout << endl; } return 0; } |