#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Tire
{
char *str;
Tire *next[26];
}*tire;
void init()
{
tire=new Tire;
for(int i=0;i<26;i++)
tire->next[i]=NULL;
tire->str=NULL;
}
void insert(char *p,char *t)
{
int len=strlen(p);
Tire *cur,*newtire;
cur=tire;
for(int i=0;i<len;i++)
{
int tem=p[i]-'a';
if(cur->next[tem]==NULL)
{
newtire=new Tire;
for(int i=0;i<26;i++)
newtire->next[i]=NULL;
newtire->str=NULL;
cur->next[tem]=newtire;
}
cur=cur->next[tem];
}
cur->str=new char[16];
strcpy(cur->str,t);
}
void find(char *p)
{
Tire *cur;
cur=tire;
int len=strlen(p);
for(int i=0;i<len;i++)
{
int tem=p[i]-'a';
if(cur->next[tem]==NULL)
{
printf("%s",p);
return ;
}
cur=cur->next[tem];
}
if(cur->str)
printf("%s",cur->str);
else
printf("%s",p);
return ;
}
int main()
{
char p[3200],t[3200];
scanf("%s",p);
init();
while(~scanf("%s %s\n",t,p)&&(strcmp(t,"END")))
insert(p,t);
while(gets(p)&&strcmp(p,"END"))
{
char tem[3200];
int len=strlen(p);
int tot=0;
for(int i=0;i<len;i++)
{
if(p[i]<='z'&&p[i]>='a')
tem[tot++]=p[i];
else
{
tem[tot]=0;
find(tem);
// printf("tem=%s\n",tem);
tot=0;
printf("%c",p[i]);
}
}
printf("\n");
}
}
hdu 1075 字典树
最新推荐文章于 2020-08-26 17:18:11 发布