#include <bits/stdc++.h> using namespace std; map<string,int> cnt; vector<string> words; string repr(const string& s) { string ans=s; for(int i=0;i<ans.length();i++) ans[i]=tolower(ans[i]); sort(ans.begin(),ans.end());//将单词排序,以便用map进行记录 return ans; } int main() { int n=0; string s; while(cin>>s) { if(s[0]=='#') break; words.push_back(s); string r=repr(s);//利用repr进行标准化,并将其用map记录个数 if(!cnt.count(r))//若cnt中没有r cnt[r]=0; cnt[r]++; } vector<string> ans; for(int i=0;i<words.size();i++) if(cnt[repr(words[i])]==1)//寻找map中单词是否出现过 ans.push_back(words[i]);//若出现过将其插入在vector中 sort(ans.begin(),ans.end());//将vector中的单词按字典序排序 for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl; return 0; }
刚开始写的时候没有在最后一个If语句里对words里的单词进行初始化,WA了两次