首页 > 其他 > 详细

Hat’s Words(字典树)

时间:2015-10-28 21:08:23      阅读:267      评论:0      收藏:0      [点我收藏+]

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11314    Accepted Submission(s): 4041


Problem Description
A 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.
 

 

Input
Standard 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.
 

 

Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 

 

Sample Input
a ahat hat hatword hziee word
 

 

Sample Output
ahat hatword
 
题解:加了个val判断是否为一个完整的单词;本来还以为是只与hat结合呐,神经的只减去hat,谁知道,自己想错了。。。是两个单词的结合;
代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 const int INF=0x3f3f3f3f;
 9 const double PI=acos(-1.0);
10 const int MAXN=50010;
11 int ch[MAXN][30],word[MAXN],val[MAXN];
12 char dt[50010][110];
13 int sz;
14 void initial(){
15     sz=1;
16     mem(ch[0],0);mem(word,0);mem(val,0);
17 }
18 void join(char *s){
19     int len=strlen(s),k=0,j;
20     for(int i=0;i<len;i++){
21         j=s[i]-a;
22         if(!ch[k][j]){
23             mem(ch[sz],0);
24         //    val[sz]=0;
25             ch[k][j]=sz++;
26         }
27         k=ch[k][j];
28         word[k]++;
29     }
30     val[k]=1;
31 }
32 bool find(char *s){
33     int len=strlen(s),k=0,j;
34     for(int i=0;i<len;i++){
35         j=s[i]-a;
36         k=ch[k][j];
37         if(!word[k])return false;
38     }
39     if(val[k])return true;
40     else return false;
41 }
42 int main(){
43     initial();
44     int t=0;
45     char s[110],l[110],r[110];
46     while(~scanf("%s",dt[t]))join(dt[t++]);
47     for(int i=0;i<t;i++){
48         int len=strlen(dt[i]);
49         if(len<=1)continue;
50         for(int j=1;j<len;j++){
51             strcpy(r,dt[i]+j);
52             strcpy(l,dt[i]);
53             l[j]=\0;
54             if(find(l)&&find(r)){
55                 puts(dt[i]);break;
56             }
57         }
58     }
59     return 0;
60 }

 

Hat’s Words(字典树)

原文:http://www.cnblogs.com/handsomecui/p/4918496.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!