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

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

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9574    Accepted Submission(s): 3421
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
 
a ahat hat hatword hziee word
 
Sample Output
 
ahat hatword
 
Author
戴帽子的
 

Recommend

题意:推断一个单词能不能有两个单词组成,能够的话就输出。

题解:全部单词建成一颗字典树,单词插入结尾记录单词的个数。查询时直接暴力拆单词推断。

#include
#include
#include
#include
#include
#define N 50020using namespace std;char s[N][42];struct Trie { int num; struct Trie *nxt[26]; Trie() { num=0; for(int i=0; i<26; i++) { nxt[i]=NULL; } }};void Trie_Inser(Trie *p,char s[]) { int i=0; Trie *q=p; while(s[i]) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL) { q->nxt[nx]=new Trie; } i++; q=q->nxt[nx]; } q->num+=1;}bool Trie_Serch(Trie *p,char s[],int be,int en) { Trie *q=p; int i=be; while(i<=en) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL)return false; q=q->nxt[nx]; i++; } if(q->num>0)return true; return 0;}int main() { //freopen("test.in","r",stdin); Trie *p=new Trie; int id=1; while(~scanf("%s",s[id])) { Trie_Inser(p,s[id]); id++; } for(int i=1; i

转载地址:http://dxnal.baihongyu.com/

你可能感兴趣的文章
OSChina 周四乱弹 ——小小编辑教你装逼斗气
查看>>
CRS-4402(Doc ID 1212703.1) 续
查看>>
Maven项目中添加jFinal包以及源文件
查看>>
Android实用笔记——使用ViewPager实现导航
查看>>
Orcale无奈的Char与Varchar
查看>>
深入理解Java虚拟机 读书笔记 之 how to STW
查看>>
有关数据库事务的一些理解
查看>>
clang: error: exit code 1 错误详解
查看>>
MyEclipse Web Project转Eclipse Dynamic Web Project
查看>>
ELK之权限管理
查看>>
×_7_12_2013 I: Light on or off
查看>>
mac下idea性能优化
查看>>
eclipse格式化代码不自动换行方法
查看>>
eclipse运行的第一个hello-jni程序
查看>>
Javascript使用包含条件控制字段必填
查看>>
mysql grant 命令详解
查看>>
SpringCloud(第 052 篇)CentOS7 安装 Docker 以及常用操作命令讲解
查看>>
git介绍及相关命令
查看>>
标准库priority_queue的一种实现
查看>>
Linux哲学思想:组合小软件完成大任务
查看>>