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

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

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

 

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: 2486Memory: 9096 KB		Time: 31 MSLanguage: G++		Result: Accepted*///这道题目思路非常easy//就是找两个单词组成还有一个单词就可#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int MAXN = 1000000 + 5;const int MAXM = 50000 + 5;const int INF = 0x3f3f3f3f;struct node { int v,next[30]; void init() { v = -1; memset(next, -1, sizeof(next)); }} L[MAXN];int tot;char str[MAXM][100];priority_queue
, greater
>P;void add(char * a, int len) { int now = 0; for(int i = 0; i < len ; i ++) { int tmp = a[i] - 'a'; int next = L[now].next[tmp]; if(next == -1) { next = ++ tot; L[next].init(); L[now].next[tmp] = next; } now = next; } L[now].v = 0;}bool querys(char * a) { int len = strlen(a); int now = 0; for(int i = 0; i < len; i ++) { int tmp = a[i] - 'a'; int next = L[now].next[tmp]; if(next == -1) return false; now = next; } return L[now].v == 0;}bool query(char * a, int len) { int now = 0; for(int i = 0; i < len; i ++) { int tmp = a[i] - 'a'; int next = L[now].next[tmp]; if(L[now].v == 0) { if(querys(a + i)) return true;//是否存在还有一个单词能够组成剩下的字符串 } now = next; } return false;}int main() { int cnt = 0, Min = INF; L[0].init(); tot = 0; //freopen("D://imput.txt", "r", stdin); while(~scanf("%s", str[cnt ++])) { add(str[cnt - 1], strlen(str[cnt - 1])); Min = min(Min, int(strlen(str[cnt - 1]))); } for(int i = 0; i < cnt ; i ++) { if(Min * 2 > strlen(str[i]))continue; if(query(str[i], strlen(str[i]))) P.push(str[i]); } string s; while(!P.empty()) {//优先队列自己主动排序也可使用set或者map s = P.top(); P.pop(); cout<
<

转载于:https://www.cnblogs.com/gavanwanggw/p/7262394.html

你可能感兴趣的文章
通过JS语句判断WEB网站的访问端是电脑还是手机
查看>>
(8) iphone 开发 数据传递 : 02 页面切换与数据的反向传递
查看>>
LPC3250 External Memory Controller
查看>>
MySQL内存表的弊端
查看>>
使用SIMILE Timeline 将邮件“事件”可视化
查看>>
SQL:创建某一时间段内的周末日期表以及特殊处理日期表
查看>>
什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?(转)
查看>>
LindAgile.SchedulingTask~设计一个不错的任务调度组件
查看>>
恶搞之手机垃圾信息发送器 手机短信骚扰器
查看>>
mysql replication之binlog-do-db、binlog-ignore-db
查看>>
Date类型和Long类型的相互转换
查看>>
XMPP协议
查看>>
CSS:给 input 中 type="text" 设置CSS样式
查看>>
Softmax函数
查看>>
hdu4462 Scaring the Birds
查看>>
设计中的道理_6
查看>>
MFC——AfxParseURL用法
查看>>
关于综合布线系统线缆挑选方法
查看>>
面向过程,面向对象,函数式对同一个问题的思考方式
查看>>
盘点:抵御网络攻击哪国强?世界20强国排名
查看>>