HDU-1247 Hat's Words (字典树)
来源:互联网 发布:exe电子书制作软件 编辑:程序博客网 时间:2024/06/07 20:17
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12123 Accepted Submission(s): 4329
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.
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.
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
Author
戴帽子的
字典树的具体用法可看:http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html
贴个模板学习一下。
#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>using namespace std;#define INF 0x3f3f3f3const int N=50005;const int mod=1e9+7;typedef struct Trie{ Trie *next[26]; bool v;}Trie;Trie *root=(Trie *)malloc(sizeof(Trie));void insert(char *str){ int len=strlen(str); Trie *p = root,*q; for (int i=0; i<len; i++) { int id=str[i]-'a'; if (p->next[id]==NULL) { q=(Trie *)malloc(sizeof(Trie)); q->v=0; for (int j=0; j<26; j++) q->next[j]=NULL; p->next[id]=q; } p=p->next[id]; } p->v=true;}int find(char *str){ int len=strlen(str); Trie *p = root; for (int i=0; i<len; i++) { int id=str[i]-'a'; p=p->next[id]; if (p==NULL) return 0; } return p->v;}void del(Trie *root){ for (int i=0; i<26; i++) { if (root->next[i]!=NULL) { del(root->next[i]); } } free(root);}char s[50005][50];int main() { int count=0; char str[50]; for (int i=0; i<26; i++) { root->next[i]=NULL; } root->v=0; while (scanf("%s",str)!=EOF) { strcpy(s[count++], str); insert(str); } for (int i=0; i<count; i++) { for (int j=1; j<strlen(s[i]); j++) { char temp1[50]={},temp2[50]={}; strncpy(temp1, s[i], j); strncpy(temp2, s[i]+j, strlen(s[i])-j); if (find(temp1)&&find(temp2)) { printf("%s\n",s[i]); break; } } } del(root); return 0;}
0 0
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- 【字典树】HDU 1247 Hat’s Words
- HDU--1247:Hat’s Words (字典树)
- hdu 1247 Hat’s Words(字典树)
- [字典树] HDU 1247 - Hat’s Words
- hdu 1247 Hat’s Words (字典树)
- HDU 1247 Hat’s Words 字典树
- HDU - 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words(字典树)
- Implementing Angular2 forms – Beyond basics
- 函数fork()与vfork()的对比
- Spring Security 3.x 完整入门教程
- iOS开发资源:VideoPlayerKit、AHAlertView以及REMenu等
- 底部导航栏中间凸起的菜单键实现
- HDU-1247 Hat's Words (字典树)
- 二叉树的深度
- Android WebView属性及用法
- ThinkPHP学习--语法学习
- Java-----隐藏手机号中间四位
- Hadoop 学习笔记六 分布式文件系统HDFS
- 遍历Map的四种方法
- 判断正方形
- Caused by: java.lang.IllegalArgumentException:Receiver not registered: 解决办法