uva 10391 Compound Words (字符串-hash)
Problem E: Compound Words
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is theconcatenation of exactly two other words in the dictionary.
Input
Standard input consists of a number of lowercase words, one per line,in alphabetical order. There will be no more than 120,000 words.
Output
Your output should contain all the compound words, one per line, inalphabetical order.
Sample Input
a alien born less lien never nevertheless new newborn the zebra
Sample Output
alien newborn
题目大意:
有一堆按照字典序排好的字符串,问你有多少字符串是由其它两个字符串组成。
解题思路:
如果用两个字符串拼接看拼接好的字符串是否在字典中,一定会超时。
我们可以逆向,由于字符串的长度不是很长,所以把一个字符串拆为两个字符串看这两个字符串是否都在字典中即可
解题代码一:
判断字符串是否在字典中,可以用STL set,也是轻松AC
#include <iostream> #include <set> #include <cstdio> #include <string> using namespace std; set <string> mys; int main(){ string st; set <string>::iterator it; while(cin>>st) mys.insert(st); for(it=mys.begin();it!=mys.end();it++){ st=*it; for(int i=0;i<st.length()-1;i++){ string sub1=st.substr(0,i+1); string sub2=st.substr(i+1,st.length()-(i+1)); if( mys.find(sub1)!=mys.end() && mys.find(sub2 )!=mys.end() ){ printf("%s\n",st.c_str()); break; } } } return 0; }
解题代码二:
判断字符串是否在字典中,可以用hash,于是手写hash,速度会比set快一点,关键看hash编码
一些编码方法:点击查看
#include <iostream> #include <string> #include <cstdio> using namespace std; const int maxn=1000003; int head[maxn],next[maxn]; string str[maxn]; int cnt; void initial(){ cnt=0; for(int i=0;i<maxn;i++){ head[i]=-1; } } /*int gethash(string st){//BKDRHash 0.146s unsigned int sum=0,step=131; for(int i=0;i<st.length();i++){ sum+=st[i]*step; } return (sum & 0x7FFFFFFF)%maxn; }*/ int gethash(string st){//DJBHash 0.112s unsigned int sum=0; for(int i=0;i<st.length();i++){ sum += (sum << 5) +st[i]; } return (sum & 0x7FFFFFFF)%maxn; } void add(string st){ int c=gethash(st); str[cnt]=st; next[cnt]=head[c]; head[c]=cnt++; } bool canfind(string st){ int c=gethash(st); for(int i=head[c];i!=-1;i=next[i]){ if(str[i]==st) return true; } return false; } int main(){ string st; initial(); while(cin>>st) add(st); for(int i=0;i<cnt;i++){ for(int k=1;k<str[i].length();k++){ string sub1=str[i].substr(0,k); string sub2=str[i].substr(k,str[i].length()-k); if(canfind(sub1) && canfind(sub2)){ printf("%s\n",str[i].c_str()); break; } } } return 0; }
解题代码三:C++ 11 hash函数
判断字符串是否在字典中,可以用hash,,速度比解题代码二快
#include <iostream> #include <functional> #include <string> #include <cstdio> using namespace std; const int maxn=1000003; int head[maxn],nextt[maxn]; string str[maxn]; int cnt; hash <string> str_hash; void initial(){ cnt=0; for(int i=0;i<maxn;i++) head[i]=-1; } void add(string st){ int c=str_hash(st)%maxn; str[cnt]=st; nextt[cnt]=head[c]; head[c]=cnt++; } bool canfind(string st){ int c=str_hash(st)%maxn; for(int i=head[c];i!=-1;i=nextt[i]){ if(str[i]==st) return true; } return false; } int main(){ initial(); string st; while(cin>>st) add(st); for(int i=0;i<cnt;i++){ for(int k=1;k<str[i].length();k++){ string sub1=str[i].substr(0,k); string sub2=str[i].substr(k,str[i].length()-k); if(canfind(sub1) && canfind(sub2)){ printf("%s\n",str[i].c_str()); break; } } } return 0; }