#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;
struct treenode{
bool is_over;
char english[12];
treenode *next[26];
treenode(){
is_over = false;
memset( english, 0, sizeof( english ) );
memset( next, 0, sizeof( next ) );
}
}*root = new treenode();
char book[3001];
void add( char *martian, char *english ){
treenode *next = root;
while( *martian ){
if( next->next[(*martian)-'a'] == NULL ){
next->next[(*martian)-'a'] = new treenode();
}
next = next->next[(*martian)-'a'];
martian++;
}
next->is_over = true;
strcpy( next->english, english );
}
bool search( char *martian ){
treenode *next = root;
while( *martian ){
if( next->next[(*martian)-'a'] == NULL ){
return false;
}
next = next->next[(*martian)-'a'];
martian++;
}
if( next->is_over ){
printf( "%s", next->english );
return true;
}
return false;
}
int main(){
char martian[13], english[13], temp[130], *temp1, temp2;
scanf( "%s", temp );
while( scanf( "%s%s", english, martian ), strcmp( english, "END" ) != 0 ){
add( martian, english );
}
getchar();
while( gets(book), strcmp( book, "END" ) != 0 ){
char *iter = book;
while( *iter ){
int i = 0;
temp1 = iter;
while( isalpha( *iter ) ){
temp1[i] = *iter;
iter++;
i++;
}
temp2 = temp1[i];
temp1[i] = '\0';
if( search( temp1 ) == false ){
printf( "%s", temp1 );
}
temp1[i] = temp2;
while( !isalpha( *iter ) ){
if( *iter == NULL ){
break;
}
printf( "%c", *iter );
iter++;
}
}
puts("");
}
return 0;
}
hdu 1075 字典树
最新推荐文章于 2016-11-10 17:48:32 发布