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下也行,把每个 ...
随机推荐
- Android的一种MVP模式框架
今天给大家分享的是一种将view的初始化和逻辑与activity分离的架构,采用的是mvp模式.但令人遗憾的是,这仅仅是一个新的思路,我在实际使用中发现其并不能完全将UI逻辑与activity分开,所 ...
- Ubuntu固定ip和dns配置和查看
1.查看dns: cat /etc/resolv.conf 2.Ubuntu固定ip sudo vim /etc/network/interfaces 修改如下部分: auto p3p1 iface ...
- flask 链接 url_for()
通常html的文件都放在template里面,那么静态的文件放在哪呢?staitc里面 调用 url_for('static', filename='css/styles.css', _externa ...
- Javaweb Servlet出现Class xxx is not a servlet错误原因
- putty快速设置本地代理
sudo plink -D 127.0.0.1:8888 -l root -P 443 -pw xxx 104.xxx.xxx.xxx
- sublime text 个性设置
http://stackoverflow.com/questions/13781833/sublime-text-2-how-to-change-the-font-size-of-the-file-s ...
- ndk学习5: ndk中使用c++
默认情况下ndk不支持标准C++库,异常, rtti等 在ndk文档有关于C++ support的详细介绍 一. 使用C++标准库 介绍: 默认是使用最小额度的C++运行时库, 在Applic ...
- 使用Axis2编写webservice客户端,服务端
1.编写客户端 Axis2开发WebService客户端 的3种方式 [参考帖子] http://blog.csdn.net/wangjinwei6912/article/details/851259 ...
- windows下安装Apache 64bit
文件下载:http://pan.baidu.com/s/1c0oDjFE 一.Apache的安装 http://www.blogjava.net/greatyuqing/archive/2013/02 ...
- 初识Python(一)
Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...