UVA 156 Ananagrams
把每个单词“标准化“,即全部转化为小写再排序,然后放到map中统计。
1 #include "iostream" 2 #include "string" 3 #include "cctype" 4 #include "vector" 5 #include "map" 6 #include "algorithm" 7 using namespace std; 8 map<string, int> cnt; 9 vector<string> words; 10 string repr(const string& s)//化为小写 11 { 12 string ans = s; 13 for (int i = 0; i < ans.length(); i++) 14 { 15 ans[i] = tolower(ans[i]); 16 } 17 sort(ans.begin(), ans.end()); 18 return ans; 19 } 20 int main() 21 { 22 int n = 0; 23 string s; 24 while (cin>>s) 25 { 26 if (s[0] == '#') 27 break; 28 words.push_back(s);//压进去 29 string r = repr(s);//化为小写 30 if (!cnt[r]) 31 cnt[r] = 0; 32 cnt[r]++; 33 } 34 vector<string> ans; 35 for (int i = 0; i < words.size(); i++) 36 { 37 if (cnt[repr(words[i])] == 1)//重复度为1则为答案 38 ans.push_back(words[i]); 39 sort(ans.begin(), ans.end());//排序 40 } 41 for (int i = 0; i < ans.size(); i++) 42 { 43 cout << ans[i] << endl; 44 } 45 return 0; 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步