Hat’s Words(字典树)
来源:互联网 发布:电子狗数据更新 编辑:程序博客网 时间:2024/06/09 18:26
G - Hat’s Words
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
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
ahat
hatword
题意: 题目给出不超过50,000个英文单词表,问在这些单词中,是否有一类单词是由两个单词组合而成的,且这些单词都在单词表中.
题解:简单的字典树问题,按字符串长度大小从小到大排序,再建字典树.在插入操作时,如果发现当前单词的前缀是一个完整的单词,则dfs搜索,看是否后半部分是不是一个完整的单词就可以了. 最后把所有符合条件的单词按字典树排序输出就行.
AC代码:
hatword
题意: 题目给出不超过50,000个英文单词表,问在这些单词中,是否有一类单词是由两个单词组合而成的,且这些单词都在单词表中.
题解:简单的字典树问题,按字符串长度大小从小到大排序,再建字典树.在插入操作时,如果发现当前单词的前缀是一个完整的单词,则dfs搜索,看是否后半部分是不是一个完整的单词就可以了. 最后把所有符合条件的单词按字典树排序输出就行.
AC代码:
#include <iostream>#include <cstring>#include <sstream>#include <cstdio>#include <cmath>#include <bitset>#include <map>#include <queue>#include <vector>#include <cstdlib>#include <algorithm>#define ls u << 1#define rs u << 1 | 1#define lson l, mid, u << 1#define rson mid + 1, r, u << 1 | 1#define INF 0x3f3f3f3f#define MAX 26using namespace std;typedef long long ll;typedef unsigned long long ull;const int M = 5e4 + 100;const int mod = 1e9 + 7;bool flag;string s[M],res[M];struct Trie { int index; Trie *next[MAX]; Trie() { index = 0; memset(next,0,sizeof(next)); }};Trie *root = new Trie;bool cmp(string a,string b) { //if(a.size() == b.size()) return a < b; return a.size() < b.size();}bool cmp1(string a,string b) { return a < b;}void dfs(Trie *tr,int len,int i) { int u = s[i][len] - 'a'; if(tr->index && !s[i][len]) { flag = true; return; } if(!s[i][len]) return; if(tr->next[u]) dfs(tr->next[u],len + 1,i);}void Trie_insert(Trie *tr,int len,int i) { if(s[i][len]) { int u = s[i][len] - 'a'; if(tr->next[u] == 0) tr->next[u] = new Trie; else if(tr->next[u]->index) { if(!flag) dfs(root,len + 1,i); } Trie_insert(tr->next[u],len + 1,i); } else tr->index = 1;}int main() { //freopen("in","r",stdin); ios::sync_with_stdio(false); int n = 0; while(cin>>s[n]) { n++; } sort(s,s + n,cmp); int to = 0; for(int i = 0; i < n; i++) { flag = false; Trie_insert(root,0,i); if(flag) res[to++] = s[i]; } sort(res,res + to,cmp1); for(int i = 0; i < to; i++) cout<<res[i]<<endl; return 0;}
0 0
- Hat’s Words(字典树)
- Hat’s Words(字典树)
- hdu Hat‘s Words(字典树)
- 【字典树】 hdu1247 Hat’s Words
- Hat’s Words hdu1247 字典树+搜索
- HDU 1247 Hat’s Words(字典树)
- hdu 1247 字典树 Hat’s Words
- HDU1247-Hat’s Words(字典树)
- HDU 1247 Hat’s Words 字典树
- Hat’s Words(hdu1247字典树)
- hdu1247 Hat’s Words (字典树)
- hdu 1247 Hat’s Words 字典树
- HDU 1247 Hat’s Words(字典树)
- hdu1247 Hat’s Words(字典树)
- [HDOJ 1247] Hat’s Words [字典树]
- hdu 1247 Hat’s Words(字典树)
- hdu 1247 Hat’s Words字典树
- hdu 1247 Hat’s Words(字典树)
- 设计模式学习笔记--解释器模式Interpreter与访问者模式Visitor(Java版)
- Spark访问HDFS
- TCP基础编程的一般步骤
- javaI/O技术
- PAT(B)1010. 一元多项式求导 (25)
- Hat’s Words(字典树)
- web -inf
- Ubuntu下安装pip的方法
- iOS开发 -创建一个简单的TableView
- C++操作符++/--的那些事
- 手机SEO最新问题和答案
- perl学习笔记八——文件读写
- LeetCode Swap Nodes in Pairs
- java多态性总结