Hdu 1247 - Hat’s Words (字典树)
2017-07-20 14:40
387 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
题目大意:
给出一个单词表,询问其中是否存在某些单词,如hat-word,是由表中其他两个单词拼接得到的,输出所有的这样的单词
分析:
直接将单词全部插入字典树,然后暴力枚举每个单词的所有分割可能即可
代码:
http://acm.hdu.edu.cn/showproblem.php?pid=1247
题目大意:
给出一个单词表,询问其中是否存在某些单词,如hat-word,是由表中其他两个单词拼接得到的,输出所有的这样的单词
分析:
直接将单词全部插入字典树,然后暴力枚举每个单词的所有分割可能即可
代码:
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<map> #include <string> using namespace std; typedef long long ll; struct Trie{ Trie *next[26]; int val; Trie(){ val = -1; for (int i = 0 ; i < 26 ; i ++) next[i] = NULL; } }; void addword(string& str,Trie* node,int i) { if (node->next[str[i]-'a']==NULL) node->next[str[i]-'a'] = new Trie; node = node->next[str[i]-'a']; ++i; if (i<str.size()) addword(str,node,i); else { node->val = 1; return; } } int query(string & str ,Trie *node,int i) { if (node->next[str[i]-'a']==NULL) return -1; else node= node->next[str[i]-'a']; ++i; if (i<str.size()) return query(str,node,i); else return node->val; } string str[100000]; int main() { int cnt=0; string p,q; Trie* head = new Trie; ios::sync_with_stdio(false); while (cin>>str[cnt]) { addword(str[cnt],head,0); cnt++; } sort(str,str+cnt); for (int i = 0 ; i < cnt ; i ++) { p.clear(); q.clear(); for (int j = 0 ; j < str[i].size()-1 ;j ++) { p +=str[i][j]; q = str[i].substr(j+1); //cout<<" p = "<<p<<" q = "<<q<<endl; if (query(p,head,0)!=-1&&query(q,head,0)!=-1) { cout<<str[i]<<endl; break; } } } return 0; }
相关文章推荐
- HDU 1247 Hat’s Words【字典树】
- (HDU)1247 - Hat’s Words 【字典树】+细心 or【耍流氓大法map】
- 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/哈理工oj 2057 Hat’s Words【字典树】
- hdu 1247 Hat’s Words(字典树)
- HDU-1247 Hat's Words (字典树)
- Hat’s Words (HDU_1247) 字典树
- [字典树] HDU 1247 - Hat’s Words
- hdu 1247 Hat’s Words(字典树)
- 字典树——Hdu 1247 Hat’s Words
- HDU 1247 Hat’s Words (字典树 && map)
- HDU - 1247 Hat’s Words 字典树
- HDU-1247-Hat’s Words(字典树)
- HDU - 1247 Hat’s Words 字典树
- hdu 1247 Hat’s Words(字典树)