hdoj 1075翻译玛雅文 字典树之三

What Are You Talking About

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


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
hello, i'm from mars.
i like earth!
代码:
View Code
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct node
{
    char marth[11];
    bool has;
    struct node *next[26];
};
void insert(char *str,char *str1,node *T)
{
    node *p,*q;
    int len,id,i,j;
    p=T;
    len=strlen(str);
    for(i=0;i<len;++i)
    {
        id=str[i]-'a';
        if(p->next[id]==NULL)
        {
            q=(node*)malloc(sizeof(node));
            p->has=false;
            for(j=0;j<26;++j)
                q->next[j]=NULL;
            p->next[id]=q;
        }
        p=p->next[id];
    }
    p->has=true;
    strcpy(p->marth,str1);
}

void find(char *str,node *T)
{
    int len,i,id,flag;
    node *p;
    p=T;
    len=strlen(str);
    flag=0;
    for(i=0;i<len;++i)
    {
        id=str[i]-'a';
        if(p->next[id]==NULL)
        {
            flag=1;
            break;
        }
        else
            p=p->next[id];
    }
    if(flag||!p->has)
        printf("%s",str);
    else 
        printf("%s",p->marth);
    return;
}

int main()
{
    char str[1001],str1[1001];
    node *T;
    int i,len,k;
    T=(node*)malloc(sizeof(node));
    for(i=0;i<26;i++)
        T->next[i]=NULL;

    scanf("%s",str);
    while(1)
    {
        scanf("%s",str);
        if(strcmp(str,"END")==0)
        {
            break;
        }
        scanf("%s",str1);
        insert(str1,str,T);
    }
    getchar();//吸收上面的回车
    gets(str);
    while(1)
    {
        gets(str);
        if(strcmp(str,"END")==0)
        {
            break;
        }
        len=strlen(str);
        k=0;
    //    memset(str1,'\0',sizeof(str1));
    //    strcpy(str1,"");
        for(i=0;i<len;++i)
        {
            if(str[i]>='a'&&str[i]<='z')
            {
                str1[k++]=str[i];
            }
            else
            {
                str1[k]='\0';
                if(k!=0)
                {
                    find(str1,T);
                    k=0;
                //    strcpy(str1,"");
                }
                printf("%c",str[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

主要就是取词时注意一些细节

posted @ 2013-04-18 09:36  煮人为乐  阅读(429)  评论(0编辑  收藏  举报