UVa 10391 - Compound Words 字符串hash
来源:互联网 发布:mac在美国多少钱一支 编辑:程序博客网 时间:2024/04/30 10:03
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 the concatenation 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, in alphabetical order.
Sample Input
aalienbornlesslienneverneverthelessnewnewbornthezebra
Sample Output
aliennewborn---------------
---------------
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>using namespace std;const int maxn=32111;const int maxm=11000019;int head[maxm];int next[maxm];char word[maxn][30];int hash(char *p,int prime=10000019){ unsigned int h=0,g; for (;*p;++p){ h=(h<<4)+*p; if (g=h&0xf0000000){ h=h^(g>>24); h=h^g; } } return h%prime;}void addhash(int s){ int h=hash(word[s]); next[s]=head[h]; head[h]=s;}bool searchstr(char* str){ int h=hash(str); for (int i=head[h];i!=-1;i=next[i]) { if (strcmp(str,word[i])==0) return true; } return false;}int main(){ char str[30]; int n=1; memset(head,-1,sizeof(head)); while (gets(word[n])) { addhash(n); n++; } for (int i=1;i<n;i++) { for (int j=1;j<strlen(word[i]);j++) { strcpy(str,word[i]); str[j]='\0'; if (searchstr(str)&&searchstr(word[i]+j)) { puts(word[i]); break; } } } return 0;}
- UVa 10391 - Compound Words 字符串hash
- UVA 10391 - Compound Words 字符串hash
- uva 10391 Compound Words (字符串-hash)
- UVa 10391 - Compound Words (字符串hash)
- UVA 10391 Compound Words(hash字符串)
- UVA - 10391 Compound Words(hash)
- UVa 10391 Compound Words(字符串+查找)
- UVa10391-- Compound Words(字符串hash)
- uva 10391 compound words
- UVa 10391 - Compound Words
- uva 10391 - Compound Words
- uva 10391 - Compound Words
- UVa 10391 - Compound Words
- UVa 10391 - Compound Words
- uva 10391 - Compound Words
- UVA 10391 Compound Words
- UVA 10391 - Compound Words
- Uva-10391-Compound Words
- 12、C语言。指向(只有一个字符的字符串、字符变量、一个字符)的指针变量的相同与区别
- MongoDB008 查询(一)
- 13、C语言中动态内存分配与指向它的指针变量
- Windows7激活信息备份方法 源于Vista的方法
- 14-1
- UVa 10391 - Compound Words 字符串hash
- 14、C语言中的空类型指针
- 15、C语言用户自定义数据类型
- 16、C语言指针小结
- cocos2d-x中实现区域裁剪
- 17、C语言文件及文件操作
- 隐藏UIScrollView的滚动条
- 18、C语言中预处理及位段
- 求数组中的最长平台