博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hat’s Words HDU - 1247 (字典树)
阅读量:4556 次
发布时间:2019-06-08

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

Hat’s Words HDU - 1247

题目链接:

题目:

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.
InputStandard 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
aahathathatwordhzieeword
Sample Output
ahathatword 题意:就是给你一个单词列表,然后单词可以从中间拆成任意两部分,只要这两部分都是单词列表里的就把这个单词输出,否则不输出 思路:利用字典树将所有单词插入字典树中,单词个数均赋予1,然后后面处理的时候再利用strncpy函数将单词分开,分别存入s1,s2中,注意末尾要赋予“\0” 才可使用,然后就利用字典树开始找,当find(s1)+find(s2)为2时,说明都找到了,此时将字符串输出即可;
// // Created by HJYL on 2019/8/21.//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int maxn=1e6+10;struct trie{ int sum; trie* next[26]; trie(){ for(int i=0;i<26;i++) next[i]=NULL; sum=0; }}root;void insert(char* s){ trie* p=&root; for(int i=0;s[i];i++) { if(p->next[s[i]-'a']==NULL) p->next[s[i]-'a']=new trie; //else p=p->next[s[i]-'a']; } p->sum=1;//单词存在就是1}int find(char* s){ trie* p=&root; for(int i=0;s[i];i++) { if(p->next[s[i]-'a']==NULL) return 0; else p=p->next[s[i]-'a']; } return p->sum;}int main(){ //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text", "r", stdin); char str[50007][30]; char s1[200],s2[200]; int n=0; while(~scanf("%s",str[n])) { insert(str[n]); //cout<
<

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11386549.html

你可能感兴趣的文章
php的单元测试,PHPUnit安装
查看>>
CentOS7 设置软件镜像源
查看>>
Java并发编程:并发容器之ConcurrentHashMap
查看>>
Java范例集锦(二)
查看>>
C语言变量和常量
查看>>
LInuxDay8——shell脚本编程基础
查看>>
topcoder 673
查看>>
Java中一些常用的类,包,接口
查看>>
下载特定区域内街景照片数据 | Download Street View Photos within Selected Region
查看>>
StarUML 破解方法
查看>>
C语言结构体
查看>>
[转]Tribon船体生产设计应用
查看>>
easy ui datagrid 让某行复选框不能选中
查看>>
第六周作业
查看>>
关于adb端口被占用的解决办法
查看>>
php 部分内置函数的使用
查看>>
字符串处理技巧
查看>>
归档及压缩命令
查看>>
Mybatis步骤
查看>>
WPF自定义控件之扩展原生控件
查看>>