#include"iostream"
#include"stdio.h"
#include"cstring"
#include"cstdlib"
#include"sstream"
using namespace std;
const int maxn_sigma=26;
struct trie{
int val;
trie* next[maxn_sigma];
trie(){val=0;for(int i=0;i<maxn_sigma;i++) next[i]=NULL;}
};
typedef trie node;
int idx(char c)
{
return c-'a';
}
void insert(node *root,char *s,int v)
{
int k;node* p = root;
for(int i=0;s[i];i++)
{
k=idx(s[i]);
if(p->next[k]==NULL)
{
node* q=(node*)malloc(sizeof(node));
q->val=0;for(int j=0;j<maxn_sigma;j++) q->next[j]=NULL;
p->next[k]=q;
}
p=p->next[k];
}
p->val=v;
}
int find(trie *root,char *s)
{
int k;trie *p=root;
for(int i=0;s[i];i++)
{
k=idx(s[i]);
if(!p->next[k])
return 0;
p=p->next[k];
}
return p->val;
}
int main()
{
trie tree;
node *root=&tree;
int n;
int i=1;
char s[100000+20][11],tmp[11],buf[100];
while(gets(buf))
{
if(strcmp(buf,"")==0) break;
stringstream ch(buf);
ch>>s[i];
ch>>tmp;
insert(root,tmp,i++);
}
char a[100];
while(scanf("%s",tmp)==1)
{
int res=find(root,tmp);
if(res) printf("%s\n",s[res]);
else printf("eh\n");
}
return 0;
}
POJ 2503 动态建树
最新推荐文章于 2020-01-29 21:01:00 发布