Hat’s Words(字典树)
2015-10-28 20:09
337 查看
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Your output should contain all the hat’s words, one per line, in alphabetical order.
[align=left]Sample Input[/align]
a
ahat
hat
hatword
hziee
word
[align=left]Sample Output[/align]
ahat
hatword
题解:判断单词是否是由两个单词合并得到,加了个val判断是否为一个完整的单词;本来还以为是只与hat结合呐,神经的只减去hat,谁知道,自己想错了。。。是两个单词的结合;
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; #define mem(x,y) memset(x,y,sizeof(x)) const int INF=0x3f3f3f3f; const double PI=acos(-1.0); const int MAXN=50010; int ch[MAXN][30],word[MAXN],val[MAXN]; char dt[50010][110]; int sz; void initial(){ sz=1; mem(ch[0],0);mem(word,0);mem(val,0); } void join(char *s){ int len=strlen(s),k=0,j; for(int i=0;i<len;i++){ j=s[i]-'a'; if(!ch[k][j]){ mem(ch[sz],0); // val[sz]=0; ch[k][j]=sz++; } k=ch[k][j]; word[k]++; } val[k]=1; } bool find(char *s){ int len=strlen(s),k=0,j; for(int i=0;i<len;i++){ j=s[i]-'a'; k=ch[k][j]; if(!word[k])return false; } if(val[k])return true; else return false; } int main(){ initial(); int t=0; char s[110],l[110],r[110]; while(~scanf("%s",dt[t]))join(dt[t++]); for(int i=0;i<t;i++){ int len=strlen(dt[i]); if(len<=1)continue; for(int j=1;j<len;j++){ strcpy(r,dt[i]+j); strcpy(l,dt[i]); l[j]='\0'; if(find(l)&&find(r)){ puts(dt[i]);break; } } } return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) #define P_ printf(" ") const int INF=0x3f3f3f3f; const int MAXN=1000010;//要开的足够大 int ch[MAXN][30]; int word[MAXN]; char str[50010][110]; int val[MAXN]; int sz; int N; char l[110],r[110]; void insert(char *s){ int k=0,j; for(int i=0;s[i];i++){ j=s[i]-'a'; if(!ch[k][j]){ mem(ch[sz],0); ch[k][j]=sz++; } k=ch[k][j]; word[k]++; } val[k]=1; } bool find(char *s){ int k=0,j; for(int i=0;s[i];i++){ j=s[i]-'a'; k=ch[k][j]; if(!word[k])return false; } if(val[k]!=1)return false; return true; } int main(){ int tp=0; sz=1; mem(ch[0],0);mem(val,0);mem(word,0); while(~scanf("%s",str[tp]))insert(str[tp++]); // printf("%d\n",tp); for(int i=0;i<tp;i++){ for(int j=0;str[i][j];j++){ strcpy(l,str[i]); l[j]='\0'; strcpy(r,str[i]+j); // printf("**%s %s\n",l,r); if(find(l)&&find(r)){ puts(str[i]);break; } } } return 0; }
相关文章推荐
- [学习OpenCV攻略][005][视频播放控制]
- JPA学习笔记-EntityManager
- jdbc连接数据库
- jQuery延迟对象deferred的使用
- 何时把函数放在对象中作为对象的方法
- winform 关闭窗体 释放内存
- hdu 1069 Monkey and Banana (dp)
- 对象转为字符串
- Hicharts弄个样例
- linux监控服务平台搭建过程详解
- 在java中使用JDBC访问数据库
- Math对象中的random方法的具体应用
- mac 神奇时光机
- 简易的可拖动的桌面悬浮窗效果Demo
- 5.0 快捷键
- 在CentOS上完美安装配置Solr5
- Java验证码代码
- static修饰的变量和方法直接用类进行调用,不需要先用new创建新的方法就能调用
- LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)
- 输出高效的日志信息