POJ 2001

 #include<iostream>
using namespace std;
const int kind=;
struct trienode
{
trienode * next[kind];
int branch;
trienode()
{
branch=;
for(int i=;i<kind;i++)
next[i]=NULL;
}
};
class trie
{ trienode * root;
public:
trie()
{
root=NULL;
}
void insert(char s[])
{
trienode * location = root;
if(location == NULL)
location = root = new trienode();
int i = ,k;
while(s[i])
{
k=s[i]-'a';
if(location->next[k])
location->next[k]->branch++;
else
{
location->next[k]=new trienode();
location->next[k]->branch++;
}
i++;
location = location->next[k];
}
}
int search(char s[]){
trienode * location = root;
if(!location)
return ;
int k,i = ,ans;
while(s[i])
{
k=s[i] - 'a';
if(!location->next[k])
return ;
cout<<s[i];
ans = location->next[k]->branch;
if(ans == )
return ;
location = location->next[k];
i++;
}
return ans;
} };
int main()
{
//freopen("acm.acm","r",stdin);
char a[][];
char b[];
int i = ;
int j;
int num;
trie mytrie;
while(cin>>a[i])
{
mytrie.insert(a[i]);
++ i;
}
for(j = ; j < i; ++ j)
{
cout<<a[j]<<" ";
mytrie.search(a[j]);
cout<<endl;
}
return ;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

POJ 2001

技术网站地址: vmfor.com

上一篇:codeforces 696B B. Puzzles(树形dp+概率)


下一篇:04 Python数据类型