Hat’s Words

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

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
 
Author
戴帽子的
 
简述题意: 给你很多单词,查找能够由其中的两个单词拼合而成的单词,并按字典序输出.....
 
代码:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct node
{
struct node *child[];
bool tail;
}Trie;
char mat[][];
void Insert(char *s,Trie *root)
{
int i,pos,j;
Trie *cur=root,*curnew;
for(i=;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]==NULL)
{
curnew=(Trie *)malloc(sizeof(Trie));
for(j=;j<;j++)
curnew->child[j]=NULL;
curnew->tail=false;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
}
cur->tail=true;
}
bool check(char *s,Trie *root)
{
int i,pos;
Trie *cur=root;
for(i=;s[i]!='\0';i++)
{
pos=s[i]-'a';
if(cur->child[pos]==NULL) return ;
cur=cur->child[pos];
}
if(cur->tail==)return ;
return ;
}
bool query(char *s,Trie *root)
{
int i,pos;
Trie *cur=root;
for(i=;s[i]!='\0';i++)
{
pos=s[i]-'a';
if(cur->child[pos]==NULL) return ;
else
if(cur->tail==&&check(s+i,root))
return ;
cur=cur->child[pos];
}
return ;
}
int main()
{
int i=,j;
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
Trie *root=(Trie *)malloc(sizeof(Trie));
root->tail=;
for(j=;j<;j++)
root->child[j]=NULL;
while(scanf("%s",mat[i])!=EOF){
Insert(mat[i],root);
i++;
}
for(j=;j<i;j++)
{
if(query(mat[j],root))
printf("%s\n",mat[j]);
}
return ;
}

最新文章

  1. Redux教程2:链接React
  2. SQLAchemy Core学习之Reflection
  3. php页面输出时,js设置input框的选中值
  4. IOS 沙盒机制 浅析
  5. MySQL使用rand函数实现随机数[转]
  6. [转载]Java的内存回收机制
  7. 敏捷开发的价值观(转自MBAlib)
  8. Beej网络socket编程指南
  9. Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)
  10. Windows入门基础:2.vs2013中Icon显示
  11. 使用VNC+SSH建立安全的远程桌面访问WINDOWS服务器
  12. .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
  13. 知识小罐头08(tomcat8启动源码分析 上)
  14. base64文件大小计算
  15. 第三方工具系列--Lombok常用注解
  16. go byte 和 string 类型之间转换
  17. ASP.NET MVC 右键点击添加没有区域(Area)、控制器、试图等选项
  18. 【java】[文件上传jar包]commons-fileUpload组件解决文件上传(文件名)乱码问题
  19. [笔记] imooc《JavaScript深入浅出》对象与函数
  20. linux每日命令(7):rmdir命令

热门文章

  1. postgreSql基础命令及linux下postgreSql命令
  2. Linux命令工具基础04 磁盘管理
  3. Blend操作入门: 别站在门外偷看,快进来吧!(转)
  4. s表达式和json表达式
  5. 使jQuqer更高效的方法
  6. 刻录DVD_目录
  7. 转:HTTP 301 跳转和302跳转的区别
  8. asp.net开发中经常用到的方法
  9. onTouch事件试验(覆写onTouchEvent方法,同时设置onTouchListener)
  10. 本地存储 localStorage/sessionStorage/cookie