hdu--(1247)Hat’s Words(trie树)
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
You are to find all the hat’s words in a dictionary.
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.
ahat
hat
hatword
hziee
word
#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 ;
}
hdu--(1247)Hat’s Words(trie树)的更多相关文章
- Hdu 1247 Hat's Words(Trie树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- HDU1247 - Hat’s Words(Trie树)
题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- c#中struct和class的区别 详细[转]
转自:http://blog.csdn.net/justlovepro/archive/2007/11/02/1863734.aspx 有这么几点不同: 1.struct 是值类型,class是对象类 ...
- V-rep学习笔记:转动关节1
V-REP(Virtual Robot Experimentation Platform),是全球领先的机器人及模拟自动化软件平台.V-REP让使用者可以模拟整个机器人系统或其子系统(如感测器或机械结 ...
- [HRBUST1472]Coin(dp,计数)
题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1472 题意:给n个硬币,面值随意.问恰好凑成m元的种类数(去掉重复). dp(i,j,k ...
- zoj 2107&&hdu 1007最近点对问题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds ...
- 构建工具之 - Gradle一般使用常见问答
Gradle借助Groovy语言作为其配置脚本,使得Gradle变的十分强大,几乎是无所不能,我们在掌控这个强大且功能繁多的工具时也比较困难和凌乱,因此本文记录一些使用过程常见的配置,以备不时之需! ...
- Getuserpassword
将[新注册的用户的用户名和密码]保存到服务端本地 /*将注册成功的用户名和密码保存到本地*/ /*定位*/ File f = new File("D:/lab_2/用户名和密码.qq&quo ...
- Android_安装GooglePlay
百度搜索:“google play 安装” http://jingyan.baidu.com/article/cbf0e500f4645b2eab28935a.html http://samsungb ...
- poj2187Beauty Contest(凸包直径)
链接 利用旋转卡壳 参考博客http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <iostream> ...
- sscanf的用法(转)
队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...
- ssh免密码登陆及其原理
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...