HDU 1247 Hat’s Words
来源:互联网 发布:sqlserver history 编辑:程序博客网 时间:2024/06/11 10:54
Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11887 Accepted Submission(s): 4235
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
戴帽子的
大体题意:
给你许多单词,让你找出所有的单词 满足,某个单词可拆成字典里存的单词!
思路:
方法有很多,才5W个单词,UVA 10391 和这个题是一样的,可对比看,那个12W个单词!
方法1:
建立set,把所有单词存入set里,进行二分查找!
方法2,直接建立字典树,两个方法输入基本一样!
就是查找了!
方法1: set
#include<iostream>#include<string>#include<set>#include<vector>using namespace std;int main(){ set<string>s; string p,q; vector<string>v; while(cin >> p){ s.insert(p); v.push_back(p); } for (int i = 0; i < (int)v.size(); ++i){ int len = (int)v[i].length(); for (int k = 1; k < len; ++k){ p = v[i].substr(0,k); q = v[i].substr(k); set<string>::iterator it1 = s.find(p); set<string>::iterator it2 = s.find(q); if (it1 != s.end() && it2 != s.end()){cout << v[i] << endl;break;} } } return 0;}
方法2:
字典树方法:
#include<iostream>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<vector>using namespace std;const int maxn = 26;struct Trie{ int id,v; Trie *next[maxn];};Trie *root;void createTrie(const char *str){ Trie *p = root, *q; int len = strlen(str); for (int i = 0; i < len; ++i){ int id = str[i] - 'a'; if (p->next[id] == NULL){ q = new Trie; for (int j = 0; j < maxn; ++j)q->next[j] = NULL; p->next[id]= q; p=q; }else p=p->next[id]; } p->v=-1;}int findTrie(const char *str){ Trie *p = root; int len = strlen(str); for (int i = 0; i < len; ++i){ int id = str[i] - 'a'; p=p->next[id]; if (p == NULL)return 0; } if (p->v == -1)return 1; return 0;}int main(){ root = new Trie; for (int i = 0; i < maxn; ++i)root->next[i] = NULL; vector<string>v; string s; while(cin >> s){ v.push_back(s); const char *ss = s.data(); createTrie(ss); } for (int i = 0; i < v.size(); ++i){ s = v[i]; int len = (int)s.length(); string a,b; for (int k = 1; k < len; ++k){ a = s.substr(0,k); b = s.substr(k); const char *pp1=a.data(); const char *pp2=b.data(); if (findTrie (pp1) && findTrie(pp2)){cout << s << endl;break;} } } 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 Hat's Words(1247)
- HDU 1247 Hat’s Words
- HDU 1247 Hat’s Words
- hdu 1247 Hat’s Words
- 【Java设计模式】详解系列 3.1.2行为型模式 : 观察者模式 — 模式详解
- shell 中的continue
- HDU 1005.Number Sequence【用递归会超时】(2.5)
- eval解析JSON注意点
- Theano 逻辑回归教程 的注释
- HDU 1247 Hat’s Words
- HTML5开发移动web应用——Sencha Touch篇(8)
- 自定义等高的cell(代码Autolayout)
- Android基础之Fragment2
- NSWindow.setFrame的坑
- Scanf函数的输入问题
- Open Credit System
- 查找二叉树
- HDU 4612 Warm up(边双连通分量)