例題4:UVA 156 Ananagrams
題目:把每個單詞全部轉化成小寫字母,對每個單詞,看它的字母重排後得到的單詞在所有輸入的單詞中是否出現過,若沒有出現,就輸出原單詞。所有要輸出的單詞按字典序排列輸出。
思路:先小寫化,然後二維的string排個序,然後用map建立string與int的映射,然後hash一下再放到set裡面排個序,其實不hash應該也可以,因為set本身就有去重的功能
#include <iostream>#include <string.h>#include <map>#include <set>#include <algorithm>using namespace std;int main(){ string s; string str[1025],str2[1025]; int k=0; map<string,int>hashs; set<string>jihe; while(getline(cin,s)) { if(s[0]=='#') break; string tmp; for(int i=0; i<s.size(); i++) { if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')) { tmp+=s[i]; } else { if(!tmp.empty()) { str[k++]=tmp; tmp.clear(); } } } if(!tmp.empty()) { str[k++]=tmp; tmp.clear(); } } //cout<<"****"<<endl; /*for(int kk=0; kk<k; kk++) { for(int j=0; j<str[kk].size(); j++) { if(str[kk][j]>='A'&&str[kk][j]<='Z') { str2[kk][j]=str[kk][j]+32; cout<<str[kk][j]<<" 1 "<<str2[kk][j]<<endl; //cout<<100<<endl; //cout<<<<endl; } else if(str[kk][j]>='a'&&str[kk][j]<='z') { str2[kk][j]=str[kk][j]; cout<<str[kk][j]<<" 2 "<<str2[kk][j]<<endl; //cout<<str[kk][j]<<endl; //cout<<str2[kk][j]<<endl; } cout<<str2[kk]<<" 4444 "<<endl; }//cout<<str2[kk]<<endl; cout<<str[kk]<<" 3333 "<<endl; sort(str2[kk].begin(),str2[kk].end()); hashs[str2[kk]]++; }*/ string tmp2; for(int kk=0;kk<k;kk++) { for(int j=0; j<str[kk].size(); j++) { if(str[kk][j]>='A'&&str[kk][j]<='Z') { tmp2+=str[kk][j]+32; } else if(str[kk][j]>='a'&&str[kk][j]<='z') { tmp2+=str[kk][j]; } } str2[kk]=tmp2; //cout<<tmp2<<" 100 "<<str2[kk]<<endl; tmp2.clear(); sort(str2[kk].begin(),str2[kk].end());//此處居然可以這樣排序,我還是第一次見到 //cout<<str2[kk]<<endl; hashs[str2[kk]]++; } for(int kk=0; kk<k; kk++) { //cout<<hashs[str2[kk]]<<"****"<<str2[kk]<<"***"<<str[kk]<<endl; if(hashs[str2[kk]]==1) { jihe.insert(str[kk]); } } for(set<string>::iterator it=jihe.begin(); it!=jihe.end(); it++) { cout<<*it<<endl; } return 0;}/**ladder came tape soon leader acme RIDE lone Dreis peatScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob driesladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries#*/