字典树典型应用(1247)
来源:互联网 发布:知乎电脑版下载官网 编辑:程序博客网 时间:2024/06/05 23:41
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12639 Accepted Submission(s): 4521
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
/*------------------Header Files------------------*/#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <cstdlib>#include <ctype.h>#include <cmath>#include <stack>#include <queue>#include <deque>#include <map>#include <vector>#include <set>#include <limits.h>using namespace std;/*------------------Definitions-------------------*/#define LL long long#define uLL unsigned long long#define PI acos(-1.0)#define INF 0x3F3F3F3F#define MOD 9973#define MAX 105#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r/*---------------------Work-----------------------*/char word[50050][30];struct node{bool isWord;node *next[30];node(){isWord=false;memset(next,0,sizeof(next));}};void insert(node *rt,char *s){int i=0;node *p=rt;while(s[i]){int temp=s[i++]-'a'; //所属分支if(p->next[temp]==NULL)p->next[temp]=new node();p=p->next[temp];}p->isWord=true;}bool searchWord(node *rt,char *s){stack<int>mystack;int i=0;node *p=rt;while(s[i]){int temp=s[i++]-'a';if(p->next[temp]==NULL) return false;p=p->next[temp];if(p->isWord&&s[i])mystack.push(i);}while(!mystack.empty()){bool flag=true;i=mystack.top();mystack.pop();p=rt;while(s[i]){int temp=s[i++]-'a';if(p->next[temp]==NULL){flag=false;break;}p=p->next[temp];}if(flag&&p->isWord) return true;}return false;}void work(){int i=0;node *rt=new node();while(scanf("%s",word[i])==1){insert(rt,word[i]);i++;}//for(int j=0;j<i;j++)//printf("%s\n",word[j]);for(int j=0;j<i;j++)if(searchWord(rt,word[j]))printf("%s\n",word[j]);}/*------------------Main Function------------------*/int main(){//freopen("test.txt","r",stdin);work();return 0;}
0 0
- 字典树典型应用(1247)
- 杭电1251 字典树典型应用
- uvalive 5026 字典树典型
- 典型应用
- 字典树的应用
- 字典树及其应用
- 字典树简单应用
- 字典树应用
- 字典树应用
- 字典树的应用
- 字典树应用
- HDU 1251(搜索题,典型的字典树问题)
- POJ 3283 典型的字典树trie tree 问题
- 字典树 详解 模板代码 加典型习题
- HDU1247(字典树应用)
- poj 3764字典树应用
- UVA12506 字典树简单应用
- hdu-1247 简单map的应用。(字典树)
- 你若顽强到底,一切皆有可能——谈工程师职业素养(《大话Java性能优化》前言)
- const char **ptr 与 char *ptr[] 的释放问题的探究
- MySQL入门--显示指定数据表的字段结构、插入指定字段的值(记录)、查看指定表中指定字段的所有记录
- B/S之SQL注入
- HashMap和HashSet的区别
- 字典树典型应用(1247)
- 多数据显示之强大的RecycleView 使用完全解析(初级基础篇)
- lightoj 1421 - Wavio Sequence LIS变形
- Max Factor (hdu_2710) 素数筛选
- Linux源码环境下编译apk实践
- git如何更新fork的repository
- 多线程技术(三)
- java集合总结
- 《编程珠玑》第二章三个问题A---查找40亿个32位整数中缺失的某个整数