HDU 1075 What Are You Talking About

来源:互联网 发布:孕36周b超标准数据 编辑:程序博客网 时间:2024/05/18 14:13

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 11638    Accepted Submission(s): 3722


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
STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END
 

Sample Output
hello, i'm from mars.i like earth!
Hint
Huge input, scanf is recommended.
 



1,注意输出是每输入一行就要翻译一行,不是全部输入了在翻译。
2,把火星文建成树,对应英文用数组存,num指向对应英文的序号。
3,每个翻译并输出。

#include <cstdio>#include <cstdlib>#include <cstring>char *zidian[1000000];struct node{    bool end;    int num;    node *x[26];};node *tree;char *ans[1010][3010];int cal[1010][2];char huoxing[3010];int cnt1,cnt2;void create(node *&tree){    int i;    tree=(node *)malloc(sizeof(node));    tree->end=false;    //tree->num=-1;//注意-1,和-2的不同    tree->num=-2;    for(i=0;i<26;++i)        tree->x[i]=NULL;}void insertTree(int n){    node *f=tree;    int i,j,len=strlen(huoxing);    for(i=0;i<len;++i)    {        int p=huoxing[i]-'a';        if(f->x[p]==NULL)        {            f->x[p]=(node *)malloc(sizeof(node));            f=f->x[p];            f->end=false;            //f->num=-1;            f->num=-2;            for(j=0;j<26;++j)                f->x[j]=NULL;        }        else        {            f=f->x[p];        }    }    f->end=true;    f->num=n;}char ch[20];int searchTree(){   node *f=tree;   int i,len=strlen(ch);   for(i=0;i<len;++i)   {       int p=ch[i]-'a';       if(f->x[p]==NULL)        return -1;       else        f=f->x[p];   }   if(f->end)   return f->num;   return -1;}int main(){    int i,j;    cnt1=0,cnt2=0;    create(tree);    while(scanf("%s",huoxing)&&strcmp(huoxing,"END"))    {        getchar();        if(strcmp(huoxing,"START")==0)              continue;        ++cnt1;        zidian[cnt1]=(char *)malloc(sizeof(char)*10);//字典从1开始        strcpy(zidian[cnt1],huoxing);       // printf("%s   %d     ",zidian[cnt1],cnt1);        scanf("%s",huoxing); getchar();       // printf("%s\n",huoxing);        insertTree(cnt1);    }    getchar();    while(gets(huoxing)&&strcmp(huoxing,"END"))//正文从1,1开始    {        if(strcmp(huoxing,"START")==0)              continue;        int len=strlen(huoxing);        i=0;        int p=0;        int flag;        while(i!=len)        {            if(huoxing[i]>='a'&&huoxing[i]<='z')            {                p=-1;                for(;i<len;++i)                {                    if(huoxing[i]<'a'||huoxing[i]>'z')                    {                        break;                    }                    ++p;                    ch[p]=huoxing[i];                }                ++p;                ch[p]='\0';                flag=searchTree();                ++cnt2;                if(flag==-1)//没有该火星文                {                    printf("%s",ch);                }                else                {                    printf("%s",zidian[flag]);                }            }            else            {                for(;i<len;++i)                {                    if(huoxing[i]>='a'&&huoxing[i]<='z')                    {                        break;                    }                    printf("%c",huoxing[i]);                }            }        }         printf("\n");    }    return 0;}


0 0
原创粉丝点击