hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7282 Accepted Submission(s): 2639
You are to find all the hat’s words in a dictionary.
Only one case.
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std; struct Tire{
bool isw; //是否是一个单词
Tire *next[];
Tire()
{
isw = false;
int i;
for(i=;i<;i++)
next[i] = NULL;
}
};
Tire root; void Insert(char word[]) //将word插入到字典树中,在最后标记这是一个单词
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i]-'a';
if(p->next[t]==NULL) //如果没有对应的节点
p->next[t] = new Tire;
p = p->next[t];
}
p->isw = true; //标记到这为止是一个单词
} bool isWord(char str[]) //判断这个字符串是否为一个单词
{
Tire *p = &root;
int i;
for(i=;str[i];i++){
int t = str[i]-'a';
if(p->next[t]==NULL) //如果没有对应的节点
return false;
p = p->next[t];
}
return p->isw;
}
char word[][]; //字典 int main()
{
int size=,i,j; //字典大小 while(scanf("%s",word[size])!=EOF){ //读入字典
//if(word[size][0]=='0') break;
Insert(word[size++]);
}
size--; //检查每一个单词,判断这个单词是否为Hat's word
for(i=;i<=size;i++){
for(j=;j<strlen(word[i]);j++){
char t[],*p=word[i];
strncpy(t,word[i],j);
t[j]='\0';
p+=j;
if(isWord(t) && isWord(p)){ //如果这两部分都是单词,说明是Hat's word
printf("%s\n",word[i]);
break;
}
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1247:Hat’s Words(字典树,经典题)的更多相关文章
- 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 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- 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 ...
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- hdu1305 字典树水题
题意: 给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路: 字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...
随机推荐
- BZOJ 2541: [Ctsc2000]冰原探险
Descrption 有一些矩形障碍,碰到障碍会停下,求从一个点到另一个点的最少移动步数. Sol BFS. 因为题目的特殊性质,两个矩形没有任何相邻,起始点和终点和矩形没有相邻. 所以从一个点的移动 ...
- BZOJ 1419: Red is good
Sol 期望DP. \(f[i][j]\) 表示剩下 \(i\) 张红牌, \(j\) 张黑牌的期望. 有转移方程. \(f[i][j]=0,i=0\) 没有红色牌了,最优方案就是不再翻了. \(f[ ...
- django缓存
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...
- 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件
具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...
- LUA require 搜索路径指定方法
如果是一个 *.LUA 的文件, 里面用到了自己写的库, 或者第三方写的库, 但是你不想把它放到 lua 的安装目录里, 则在代码里面可以指定require搜索的路径. package.path = ...
- React JS快速入门教程
翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...
- Eclipse CDT “Symbol NULL could not be resolved”
在ubuntu里装的eclipse C/C++版,交叉编译程序时,总是提示Symbol NULL could not be resolved.Symbol size_t could not be re ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误
今天写脚本遇到Can't use string ("bond2 Link encap:InfiniBand ") as a symbol ref while "s ...
- FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照
1 FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照 2016.02.18 by 1CM 2 T.. = Timeline support 3 支持时间轴 4 .S. = ...