HDU 1247 Hat’s Words
2016-02-05 20:15
337 查看
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.
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.
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
Sample Input
a ahat hat hatword hziee word
Sample Output
ahat hatword
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; }
相关文章推荐
- WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)
- Scanf函数的输入问题
- NSWindow.setFrame的坑
- HTML5开发移动web应用——Sencha Touch篇(8)
- 自定义等高的cell(代码Autolayout)
- Android Studio之提高编译的效率
- Fill the Square
- Theano 逻辑回归教程 的注释
- Linux查看系统信息小命令
- HDU 1005.Number Sequence【用递归会超时】(2.5)
- HDU3501 Calculation 2(欧拉函数)
- 第一次在ubuntu配置apache+php+mysql环境运行laravel5.1
- 例题6-7 UVa122 Trees on the level(树&&队列BFS&&sscanf字符串转整数)
- shell 中的continue
- HDOJ 5606-tree【并查集】
- C# Tips:获得当前登录计算机的用户(本地用户/域用户)
- 自定义等高的cell(代码创建frame)
- Python lambda和reduce函数
- 应聘者页面——文本输入框及单选多视图重构
- 重定向操作(C语言)