这字符串处理够麻烦的,不过居然都可以用库函数解决,倘若是手动对字符串进行预处理,真是难以想象啊
不过,也不可否认,内存开了超大的,时间也差点超了……
#include<iostream> #include<string> #include<map> using namespace std; map<string, int> s; string tele[1000000]; int main() { s.clear(); int n, k = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> tele[i]; while (tele[i].find('-') != string::npos)//若查找失败,返回string::npos tele[i].erase(tele[i].find('-'), 1);//删除从‘-’起的一个字符 for (int j = 0; j < tele[i].size(); j++) { switch (tele[i][j]) { case 'A': case 'B': case 'C': tele[i][j] = '2'; break; case 'D': case 'E': case 'F': tele[i][j] = '3'; break; case 'G': case 'H': case 'I': tele[i][j] = '4'; break; case 'J': case 'K': case 'L': tele[i][j] = '5'; break; case 'M': case 'N': case 'O': tele[i][j] = '6'; break; case 'P': case 'R': case 'S': tele[i][j] = '7'; break; case 'T': case 'U': case 'V': tele[i][j] = '8'; break; case 'W': case 'X': case 'Y': tele[i][j] = '9'; break; } } tele[i].insert(3, "-");//在第三个字符后面添加‘-’ s[tele[i]]++;//键值加一,也就是号码重复的次数累加 } for (map<string, int>::iterator iter = s.begin(); iter != s.end(); iter++) { if (iter->second > 1) cout << iter->first << ' ' << iter->second << endl; else k++; } if (k >= s.size()) cout << "No duplicates." << endl;//没有重复,则输出"No duplicates." return 0; }