参照了杭电某大牛 的代码,以后这就是TIRE的模板了
#include<cstdio>
#include<cstring>
#include<cctype>
#define CNUM 26
#define LMT 12
struct tnode
{
tnode *child[CNUM];
char aim[12];
bool tag;
};
tnode *root;
tnode *init(void)
{
int i;
tnode * ret=new(tnode);
ret->tag=0;
for(i=0;i<CNUM;i++)ret->child[i]=NULL;
return ret;
}
void insert(char sor[],char die[])
{
int i,len=strlen(sor);
tnode *current=root;
for(i=0;i<len;i++)
if(current->child[sor[i]-'a'])current=current->child[sor[i]-'a'];
else
{
current->child[sor[i]-'a']=init();
current=current->child[sor[i]-'a'];
}
current->tag=1;
strcpy(current->aim,die);
}
bool find(char sor[],char get[])
{
int i,len=strlen(sor);
tnode *current=root;
for(i=0;i<len;i++)
if(current->child[sor[i]-'a'])current=current->child[sor[i]-'a'];
else return 0;
if(current->tag==0)return 0;
strcpy(get,current->aim);
return 1;
}
char sec[3005];
int main()
{
int i,j,len;
char mar[LMT],ear[LMT],ans[LMT];
root=init();
scanf("%s",ear);
while(~scanf("%s",ear)&&strcmp(ear,"END"))
{
scanf("%s",mar);
insert(mar,ear);
}
scanf("%s",mar);getchar();
while(gets(sec)&&strcmp(sec,"END"))
{
j=0;len=strlen(sec);
for(i=0;i<len;i++)
if(islower(sec[i]))
mar[j++]=sec[i];
else
{
mar[j]=0;
if(j)
{
if(find(mar,ans))printf("%s",ans);
else printf("%s",mar);
}
if(sec[i])printf("%c",sec[i]);
j=0;
}
printf("\n");
}
return 0;
}