天天看點

hdu1075 What Are You Talking About (字典樹)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 18389    Accepted Submission(s): 6005

Problem Description

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

Output

In this problem, you have to output the translation of the history book.

Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END      

Sample Output

Hint

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you:  

​​1800​​​ 

​​​1247​​​ 

​​​1298​​​ 

​​​1026​​​ 

​​​1072​​ 

#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;

const int maxn=1e6;
const int max_len1=10;
const int max_len2=3e3;
int num=0,f[maxn][26];
char s[maxn][max_len1+5];
bool v[maxn];

void write(char a[],int len)
{
  int i,j,k;
  if(len>max_len1)goto d1;
  for(i=0,j=0;j<len;i=f[i][a[j++]-'a']);
  if(!v[i])goto d1;
  for(k=strlen(s[i]),j=0;j<k;j++)printf("%c",s[i][j]);
  return;
  d1:for(i=0;i<len;i++)printf("%c",a[i]);
}

int main()
{
  //freopen("1.in","r",stdin);
  
  int i,j,len;  
  char a[max_len2+5],b[max_len1+5];
  scanf("%s",a);
  while(scanf("%s%s\n",a,b),islower(a[0]))
    {
      len=strlen(b);
      for(i=0,j=0;j<len;i=f[i][b[j++]-'a'])
        if(f[i][b[j]-'a']==0)f[i][b[j]-'a']=++num;
      for(v[i]=1,len=strlen(a),j=0;j<=len;j++)s[i][j]=a[j];
  }
  while(a[0]=getchar())
    {
      if(isupper(a[0]))return 0;
      if(!islower(a[0])){printf("%c",a[0]);continue;}
      for(i=1;islower(a[i]=getchar());i++);
      write(a,i);
      if(!isupper(a[i]))printf("%c",a[i]);
  }
  return 0;
}