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 ...
随机推荐
- deeplink
http://www.cnblogs.com/shadajin/p/5724117.html Deeplink,简单讲,就是你在手机上点击一个链接之后,可以直接链接到app内部的某个页面,而不是app ...
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- rem布局原理
昨天去面试,面试官竟然说他们用媒体查询针对不同的屏幕宽度做了9个不同的rem布局,呵呵... eg: html{font-size:20px;} div{width:16rem;height:100p ...
- Windows Server 2008 IIS的备份和还原
Windows Server 2008 IIS的备份和还原 当我们服务器系统有大量的站点和虚拟目录的时候,因为种种原因需要重做系统,那么重装系统后这些站点我们是否只能一个一个的添加,如果有成百上千个站 ...
- Centos6.5使用yum安装软件的时候 Another app is currently holding the yum lock; waiting for it to exit...
Loaded plugins: fastestmirror, refresh-packagekit, security Existing . Another app is currently hold ...
- STL中一些函数的应用
1.nth_element():找到第几大的数.用法:nth_element(a,a+k,a+n),返回一个数组a中第k大的数,时间复杂度比较小,头文件#include <algorithm&g ...
- linux基础(4)-常用命令
常用命令ls ls #查看当前目录下的文件和目录 ls -l #显示详细信息 ls -a #显示所有文件 ls -t #按修改时间排序 ls -S #按文件大小排序 常用命令pwd pwd #显示 ...
- react列表中,当key改变后发生的事情
Main.jsx export default class Main extends PureComponent { constructor(props) { super(props); this.s ...
- Codeforces Round #283 (Div. 2) E. Distributing Parts 贪心+set二分
E. Distributing Parts time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- MATLAB选出一个数组里n个元素的所有组合
用nchoosek(A,n) 如: nchoosek([1,5,3,2,4,0],3) nchoosek([1,5,3,2],2)ans = 1 5 1 3 1 ...