trie树练习 Hat’s Words
2013-09-11 14:04
288 查看
Hat’s Words
TimeLimit: 2000/1000 MS
(Java/Others) Memory
Limit: 65536/32768 K (Java/Others)Total Submission(s):
2174 Accepted
Submission(s): 791
Problem DescriptionA hat’s word is a word in the dictionary that is the concatenation
of exactly two other words in the dictionary.You are to find all the hat’s words in a dictionary. InputStandard input consists of a number of lowercase words, one per
line, in alphabetical order. There will be no more than 50,000
words.Only one case. OutputYour output should contain all the hat’s words, one per line, in
alphabetical order. Sample Inputa ahat hat hatword hziee word Sample Outputahat hatword#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
int
flag;
struct node
*next[26];
};
struct node
*root;
char
s[50001][21];
struct node *chuangjian()
{
int
i;
struct node
*p;
p=(struct
node*)malloc(sizeof(struct node));
p->flag=0;
for(i=0;i<26;i++)
p->next[i]=NULL;
return
p;
}
void insert(char *s)
{
int
i;
struct node
*p;
p=(struct
node*)malloc(sizeof(struct node));
p->flag=0;
p=root;
int
len=strlen(s);
for(i=0;i<len;i++)
{
if(p->next[s[i]-'a']==NULL)
{
p->next[s[i]-'a']=chuangjian();
}
p=p->next[s[i]-'a'];
}
p->flag=1;
}
int search(char
*s)
{
int
i,n;
n=strlen(s);
struct node
*p=root;
for(i=0;i<n;i++)
{
if(p->next[s[i]-'a']==NULL)
return
0;
else
p=p->next[s[i]-'a'];
}
return
p->flag;
}
int main()
{
int
n,i,k,m,j;
char
str1[21],str2[21];
root =(struct
node*)malloc(sizeof(struct node));
for(i=0;i<26;i++)
root->next[i]=NULL;
k=1;
while(scanf("%s",s[k])!=EOF)
{
insert(s[k]);
k++;
}
for(i=1;i<k;i++)
{
n=strlen(s[i]);
for(j=1;j<n;j++)
{
for(m=0;m<j;m++)//一点点分来测试
str1[m]=s[i][m];
str1[m]='\0';//一定要加上,不然WA
if(search(str1))
{
int
p;
for(p=j;p<n;p++)
str2[p-j]=s[i][p];
str2[p-j]='\0';
if(search(str2))
{
printf("%s\n",s[i]);
break;
}
}
}
}
free(root);
return
0;
}
相关文章推荐
- 基础练习08&nbsp;回文数
- matlab每日练习&nbsp;lenght函数
- 蓝桥杯&nbsp;基础练习07&nbsp;特殊的数字
- HDU 1247 Hat’s Words && Trie(字典树)
- Red Hat Linux 9安装GCC(一)
- Red Hat Linux 9安装GCC(二)
- zoj2016--Play on Words
- trie树Message Flood
- 【练习04】 字典树 1001 Hat’s Words
- poj&nbsp;1386&nbsp;Play&nbsp;on&nbsp;Words(欧拉回路…
- 杭电 HDU 1247 ACMHat’s Words(trie树 或着STL)
- CH6_EX34&nbsp;数据结构Anyview练习
- HDU1247 - Hat’s Words(Trie树)
- Red Hat 7.2 配置c…
- ACM: uva 10391 - Compound Words
- Html&nbsp;练习
- HDU 1247 Hat's Words (字典树)
- 使用google&nbsp;Adwords&nbsp;业务要谨慎
- 基础练习13&nbsp;数列排序
- 线段树练习Balanced Lineup