Hat’s Words(字典树的运用)
个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天,
不得不佩服这些发明算法人的大脑。
这题的解决方法还是从网上找到的,还好算法是自己实现得,没错!组合体只要进行分割再暴力搜索就好了,
步骤就是根据得到的字符串建立字典树,然后一一找寻时,一次拆开,只要俩边都满足在字典树里面找的到就是我们所要找的了。
关于字典树的建立的话,因为他是不知道子树的,所以只要判断出来不存在子树就malloc一个就好了,
注意指针的指向和递归的奥妙,很多更新都是在递归中完成的,需要好好的体会。
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.
OutputYour 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
#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>
#include <queue>
#include<string>
using namespace std;
const int length=;
const int maxn=;
struct word
{
word *next[length];
bool book; };
void insertword(word *root,char *s)
{
if(root==NULL||*s=='\0')
return ;
word *p=root;
while(*s!='\0'){
if(p->next[*s-'a']==NULL){
word *q=(word *)malloc(sizeof(word));
for(int i=;i<length;i++)
q->next[i]=NULL;
q->book=false;
p->next[*s-'a']=q;
p=p->next[*s-'a'];
}
else
{
p=p->next[*s-'a']; }
s++;
}
p->book=true; }
bool check(word *root,char x[],int y,int z){
word *p=root;
for(int i=y;i<=z;i++)
{
if(p->next[x[i]-'a']==NULL) return false;
p=p->next[x[i]-'a'];
}
if(p->book==true) return true;
else return false; }
bool searchword(word *root, char *s)
{
char x[];
int i=;
while(*s!='\0')
{
x[i++]=*s;
s++;
}
for(int j=;j<i-;j++)
{
if(check(root,x,,j)&&check(root,x,j+,i-)){
return true;
}
}
return false; }
char st[][];
int main()
{
word *root=(word *)malloc(sizeof(word));
root->book=false;
int i;
for(i=;i<length;i++)
root->next[i]=NULL;
i=;
while(scanf("%s",st[i])!=EOF)
{
insertword(root,st[i]);
i++;
}
for(int j=;j<i;j++)
{
if(searchword(root,st[j]))
cout<<st[j]<<endl;
}
return ;
}
Hat’s Words(字典树的运用)的更多相关文章
- hdu 1247 Hat’s Words(字典树)
Hat's Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU 1247 - Hat’s Words - [字典树水题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...
- Hat’s Words(字典树)
Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...
- hdoj 1247 Hat’s Words(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- HDU 1247 Hat’s Words (字典树 && map)
分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入 ...
- hdu1 247 Hat’s Words(字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
随机推荐
- iOS WKWebView OC 与 JS 交互学习
我写WKWebView 想让 服务端相应 一个 方法但是不响应,根据 UIWebView 用 JSContext就能拿到响应的处理经验是不是服务端 也需要 对 WKwebView有兼容的一个写法??? ...
- Safari通过JavaScript获取系统语言
IE6 IE7 IE8 Firefox Chrome Safari Opera navigator.language undefined zh-CN zh-CN navigator.userLan ...
- 纯代码编写qt登录界面(转)
1. 新建Qt Widgets Application,项目名称为login1,在类信息页面保持类名和基类为MainWindow和QMainWindow不变,取消选择创建界面选项,如下图所示. ...
- css异步加载
<link rel="preload" href="mystyles.css" as="style" onload="thi ...
- cocos2dx打飞机项目笔记四:Enemy类和EnemyLayer类
Enemy类没什么内容,就create和init方法,根据参数来创建不同的敌机,头文件代码如下: //飞机的类型 enum planeType {smallPlane, midPlane, bigPl ...
- PHP开发环境MAMP for Windows
Windows上的整合PHP开发环境有很多,如:Windows上使用的Wampserver(http://www.wampserver.com/),跨平台的Xampp(https://www.apac ...
- IDEA配置文件的保存目录
IntelliJ IDEA 的实时代码模板保存在 /templates 目录下,其他系统目录位置如下:(因为目录名在各个系统上是一致的,建议用硬盘搜索工具搜索即可) Windows: . Linux: ...
- Windows 配置Apache以便在浏览器中运行Python script的CGI模式
打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...
- Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间
一看就知道 可以LCA判断做 也可以树链剖分拿头暴力 然而快速读入和线段树维护区间会T70 于是只能LCA? 线段树的常数不小 于是需要另外一种办法来进行区间加减和查询区间和 就是使用树状数组 这个题 ...
- Qt QFileSystemModel QDirModel 示例代码, 使用方法
1. QFileSystemModel 查看,添加 和 删除目录 2. 实现代码 dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QD ...