UVa-10391 - Compound Words
继续用STL写,虽然比较耗时,但是简洁方便,不容易出错。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<string> 6 #include<algorithm> 7 #include<set> 8 using namespace std; 9 const int maxx=120010; 10 vector<string> v; 11 set<string> ans; 12 int main() 13 { 14 //freopen("in.txt","r",stdin); 15 //freopen("out.txt","w",stdout); 16 string s; 17 while(cin>>s) v.push_back(s); 18 for(int i=0;i<v.size();i++) 19 { 20 for(int j=1;j<=v[i].size();j++) 21 { 22 string t=v[i].substr(0,j); 23 if(binary_search(v.begin(),v.end(),t)) 24 { 25 t=v[i].substr(j); 26 if(binary_search(v.begin(),v.end(),t)) 27 ans.insert(v[i]); 28 } 29 } 30 } 31 for(set<string>::iterator it=ans.begin();it!=ans.end();it++) 32 cout<<*it<<endl; 33 }