该题是一道字典树的题目,就是是要你翻译一下,如果你学了字典树就会发现这是一道水体;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct t
{
char num[12];
t *ch[26];
int flag;
}tire;
tire *empty()
{
tire *t=( tire * )malloc( sizeof( tire ) );
for( int i=0;i<26;i++ )
t->ch[i]=NULL;
memset( t->num,0,sizeof( t->num ) );
t->flag=0;
return t;
}
void build( tire *t,char c[],char num[] )
{
int i=0;
while( num[i] )
{
int x=num[i]-'a';
if( t->ch[x]==NULL )
t->ch[x]=empty();
t=t->ch[x];
i++;
}
t->flag=1;
strcpy( t->num,c );
}
bool find( tire *t, char num[] )
{
int i=0,x;
while( num[i] )
{
x=num[i]-'a';
if( t->ch[x]==NULL )
return false;
if( num[i+1]=='\0' )
{
if( t->ch[x]->flag )
{
printf( "%s",t->ch[x]->num );
return true;
}
else return false;
}
i++;
t=t->ch[x];
}
return false;
}
int main( )
{
char c[12],num[12],x;
tire *root=empty();
scanf( "%s%*c",num );
while( scanf( "%s",c ),strcmp( "END",c ) )
{
scanf( "%s",num );
build( root,c,num );
}
scanf( "%s%*c",num );
int re=0;
while( x=getchar() )
{
if( (x<='z'&&x>='a')||(x<='Z'&&x>='A') )
{
num[re++]=x;
}
else
{
num[re]='\0';
if( !strcmp( "END",num ) )
break;
if( !find( root,num ) )
printf( "%s",num );
printf( "%c",x );
re=0;
}
}
return 0;
}