数据结构之字典树---hdu1247---Hat‘s word
发布日期:2022-02-02 02:58:09 浏览次数:14 分类:技术文章

本文共 1711 字,大约阅读时间需要 5 分钟。

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
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.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
aahathathatwordhzieeword
 
Sample Output
ahathatword

代码实现:

#include
#include
#include
using namespace std;#define MAX 50005char word[MAX][27];struct node{ bool is; node *next[26]; node() { is=false; memset(next,0,sizeof(next)); }};void Insert(node *rt,char *s){ int i=0; node *p=rt; while(s[i]) { int id=s[i++]-'a'; if(p->next[id]==NULL) p->next[id]=new node; p=p->next[id]; } p->is=true; //该结点是单词的尾}int Search(node *rt,char s[]){ int i=0,top=0,stack[1000]; node *p=rt; while(s[i]) { int id=s[i++]-'a'; if(p->next[id]==NULL) return 0; p=p->next[id]; if(p->is==true && s[i]) //找到该单词含有子单词的分隔点 stack[top++]=i;//入栈 } while(top)//从可能的分割点去找 { bool ok=1; i=stack[--top]; p=rt; while(s[i]) { int id=s[i++]-'a'; if(p->next[id]==NULL) { ok=false; break; } p=p->next[id]; } if(ok && p->is)//找到最后,并且是单词的 return 1; } return 0;}int main(){ int i=0; node *rt=new node; while(gets(word[i])) { Insert(rt,word[i]); i++; } for(int j=0; j

转载地址:https://blog.csdn.net/u010368749/article/details/19541261 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:数据结构之字典树
下一篇:二叉树

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月06日 06时53分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Java Web基础入门第十讲 软件密码学基础 2021-06-30
Java Web基础入门第十一讲 配置Tomcat的HTTPS连接器 2019-04-27
如何使用Chrome浏览器查看缓存? 2019-04-27
如何通过浏览器查看保存在本地磁盘的Cookie? 2019-04-27
如何在浏览器中禁用和启用Cookie? 2019-04-27
Java Web基础入门第二十六讲 JSP技术——JSP标签(下) 2019-04-27
Java Web基础入门第二十七讲 JSP技术——EL表达式和JSTL标签快速入门 2019-04-27
解决Chrome插件安装时出现程序包无效:"CRX_HEADER_INVALID"的问题 2019-04-27
最新版本的Google Chrome浏览器如何设置网页编码? 2019-04-27
PremiumSoft Navicat for MySQL 12.1.19中文版下载安装和注册机激活教程 2019-04-27
Java Web基础入门第八十六讲 在线网上书店(一)——搭建开发环境 2019-04-27
Java Web基础入门第八十七讲 在线网上书店(二)——设计实体及其相对应的数据库表 2019-04-27
Java Web基础入门第八十八讲 在线网上书店(三)——编写dao层 2019-04-27
Java Web基础入门第八十九讲 在线网上书店(四)——编写service层 2019-04-27
Java Web基础入门第九十讲 在线网上书店(五)——实现分类管理模块 2019-04-27
Java Web基础入门第九十一讲 在线网上书店(六)——实现图书管理模块 2019-04-27
Java Web基础入门第九十二讲 在线网上书店(七)——实现订单管理模块 2019-04-27
Java Web基础入门第九十三讲 在线网上书店(八)——实现数据库管理模块 2019-04-27
Java基础加强第五讲 泛型(下)——泛型类及其应用 2019-04-27
Java Web基础入门第九十九讲 JavaMail开发——在WEB应用中集成邮件发送程序 2019-04-27