POJ2001 Shortest Prefixes 动态字典树实现
2017-08-04 20:06
471 查看
#include<iostream> #include<algorithm> #include<string> #include<cstring> using namespace std; struct node{ int word; struct node *next[30]; node(); }; node::node(){ memset(next,0,sizeof(next)); word=0; } struct node *head=new node; string word[1010]; void add(string s){ struct node *p,*q; p=head; for(int i=0;i<s.length();i++){ int to=s.at(i)-'a'; if(i!=s.length()){ if(p->next[to]==NULL){ p->next[to]=new node; } p=p->next[to]; p->word=p->word+1; } } } void getMin(string s){ struct node *p; p=head; for(int i=0;i<s.length();i++){ int to=s.at(i)-'a'; cout<<s.at(i); if(i+1!=s.length()){ if(p->next[to]==NULL) return; if(p->next[to]->word==1) return; p=p->next[to]; } } } int main(){ int n=0; while(!cin.eof()){ cin>>word ; add(word ); n++; } n--; for(int i=0;i<n;i++){ cout<<word[i]<<" "; getMin(word[i]); cout<<endl; } return 0; }
相关文章推荐
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes 字典树
- Shortest Prefixes POJ - 2001(字典树)
- [字典树] poj 2001 Shortest Prefixes
- 【POJ 2001 Shortest Prefixes】+ 字典树
- POJ 2001 Shortest Prefixes 字典树Trie
- POJ2001 - Shortest Prefixes - 字典树
- poj 2001 Shortest Prefixes 字典树的输出问题的解释~~ 2012-4-14
- poj 2001 Shortest Prefixes 字典树(模版题)
- POJ 2001 Shortest Prefixes 字典树经典题,求最短唯一前缀)
- poj 2001 Shortest Prefixes ——字典树入门
- POJ 2001-Shortest Prefixes【字典树】
- Shortest Prefixes POJ - 2001 (字典树模板题~)
- POJ 2001 Shortest Prefixes【字典树】
- [复习][poj2001]字典树(trie树)Shortest Prefixes
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- POJ 2001 Shortest Prefixes 字典树
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
- POJ 2001 Shortest Prefixes【第一棵字典树】