字符串hash uva 10391 - Compound Words

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值