hdu1247字典树
来源:互联网 发布:淘宝翡翠手镯 编辑:程序博客网 时间:2024/05/20 01:47
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6816 Accepted Submission(s): 2510
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
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 2000000000#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}struct Trie{ Trie *next[26]; bool flag;//标记当前单词是否有效,比如有aabb,查aa就不有效 Trie() { flag = false; for (int i = 0; i < 26; i++) next[i] = NULL; }};Trie root;void createTrie(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(root)); for (int j = 0; j < 26; j++) q->next[j] = NULL; q->flag=false; p->next[id] = q; p = p->next[id]; } else { p = p->next[id]; } } p->flag = true;}bool findit(char *s){ int len = strlen(s); Trie *p = &root; int id; for (int i = 0; i < len; i++) { id = s[i] - 'a'; if (p->next[id] != NULL) { p = p->next[id]; } else { return false; } } return p->flag;}void del(Trie *head){ for (int i = 0; i < 26; i++) { if (head->next[i] != NULL) del(head->next[i]); } delete(head);}void clear(){ del(&root); for (int i = 0; i < 26; i++) { root.next[i] = NULL; }}char s[50000][100] = zero;string lins[50000];int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); // freopen("C:\\Users\\Sky\\Desktop\\dabiao.txt","w",stdout);#endif int n = 0; while (scanf("%s", s[n]) + 1) { createTrie(s[n++]); } bool flag = true; int l; char ss[100]; int k = 0; int num = 0; for (int i = 0; i < n; i++) { flag = true; memset(ss, 0, sizeof(ss)); l = strlen(s[i]); k = 0; for (int j = 0; j < l - 1; j++) { ss[j] = s[i][j]; if (findit(ss) && findit(&s[i][j + 1])) { lins[num++] = string(s[i]); break; } } } for (int i = 0; i < num; i++) { printf("%s\n", lins[i].c_str()); } return 0;}
0 0
- HDU1247字典树
- hdu1247 字典树模板
- HDU1247(字典树)
- HDU1247 字典树
- hdu1247字典树
- hdu1247字典树
- 字典树hdu1247
- hdu1247字典树
- hdu1247(字典树/map)
- HDU1247(字典树应用)
- hdu1247 简单的字典树
- hdu1247(字典树+枚举)
- hdu1247 字典树或者hash
- hdu1247-字典树,单词拆分
- hdu1247+hdu1075 字典树基础
- hdu1247(字典树模板)
- 【字典树】 hdu1247 Hat’s Words
- Hat’s Words hdu1247 字典树+搜索
- TM波段组合
- TreeSet要求加入的所有元素都可以相互比较大小;可以控制比较位置来决定排序方向;hashset只能比较可相等不能比较大小
- c 语言中的位域
- java异常处理机制
- JFrame显示层次问题
- hdu1247字典树
- 用C语言编程操作计算机串口的问题
- 三角形面积
- Ubuntu下安装gnuplot4.6.0
- UltraEdit 给代码注释添加快捷键
- 使用Gitosis搭建Git服务器
- 代码输出的结果为何是这样?
- GOOSE卡设计与实现
- 常规性能优化技巧(来自u3dpro.com)