· 6 years ago · Sep 26, 2019, 06:00 PM
1#include <iostream>
2#include <vector>
3#include <algorithm>
4#include <cctype>
5#include <string>
6#include <cstring>
7
8
9using namespace std;
10
11string Remove(string& str, string charToRemove){
12 //string s = "Pineapple";
13 //char chars[] = "p";
14 for (unsigned int i = 0; i < charToRemove.size(); ++i)
15 {
16 str.erase(remove(str.begin(), str.end(), charToRemove[i]), str.end());
17 }
18
19 return str;
20}
21
22bool EndsWith (std::string const &fullString, std::string const &ending) {
23 if (fullString.length() >= ending.length()) {
24 return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
25 } else {
26 return false;
27 }
28}
29
30string ToLower(string& data){
31 std::transform(data.begin(), data.end(), data.begin(),
32 [](unsigned char c){ return std::tolower(c); });
33
34 return data;
35}
36
37string PlusReform(string& s){
38 return s.substr(0, s.find('+', 0));
39}
40
41class Group{
42public:
43 vector<string> mails;
44 bool isgmail;
45 string lower;
46
47 Group(string s)
48 {
49 mails = vector<string>();
50 mails.push_back(s);
51 lower = ToLower(s);
52
53 if (EndsWith(s, "@gmail.com")){
54 isgmail = true;
55 lower = lower.substr(0, lower.size() - 10);
56 lower = PlusReform(lower);
57 lower = Remove(lower, ".");
58 }
59 else{
60 lower = s;
61 }
62 }
63
64 bool addnew(string str){
65 str = ToLower(str);
66
67 bool endsGmail = EndsWith(str, "@gmail.com");
68
69 if (!endsGmail)
70 {
71 if (isgmail)
72 {
73 return false;
74 }
75
76 return lower == str;
77 }
78
79 if (!isgmail)
80 return false;
81
82 // deleting gmail in end
83 if (str.size() > 10){
84 str = str.substr(0, str.size() - 10);
85 }
86
87 // all before first +
88 str = PlusReform(str);
89
90 // deleted tochki
91 str = Remove(str, ".");
92
93 return lower == str;
94 }
95
96 void toString(){
97 cout << mails.size() << " ";
98
99 for (int j = 0; j < mails.size() - 1; ++j){
100 cout << mails[j] << " ";
101 }
102 cout << mails[mails.size() - 1];
103
104 cout << "\n";
105 }
106};
107
108int main()
109{
110 bool skip;
111 int k;
112 string str;
113 cin >> k;
114
115 vector<Group> groups = vector<Group>();
116
117 for (int i = 0; i < k; i++)
118 {
119 skip = false;
120 cin >> str;
121
122 for (int j = 0; j < groups.size(); j++)
123 {
124 if (groups[j].addnew(str))
125 {
126 groups[j].mails.push_back(str);
127 skip = true;
128 break;
129 }
130 }
131
132 if (skip)
133 continue;
134
135 // reached code here if no equal group found
136 groups.push_back(Group(str));
137 }
138
139 // output
140 cout << groups.size() << "\n";
141 for (int i = 0; i < groups.size(); ++i)
142 {
143 groups[i].toString();
144 }
145
146 return 0;
147}