hdu 1075 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): 10368    Accepted Submission(s): 3305

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!
Hint
Huge input, scanf is recommended.
 

利用字典建立字典树,(前面的字符串放在字典树中后面字符串的最后一个字母出,其他字母处赋值“#”)然后依次取出输入字符串中的单词,在字典树中查找,没有找到输出原字符串,还有不是字母的按原来的输出

第一次WA,没有考虑到输入的字符串中 是 字典中单词的前缀的情况,改正了一下AC了

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Tire
{
    Tire * child[26];
    char word[12];
}Tire;

Tire * root;

void insert(char *s1,char *s2)
{
    int i,j;
    Tire *p,*q;
    q = root;
    
    for(i=0;i<strlen(s2);i++)
    {
        if(q->child[s2[i]-'a']) q = q->child[s2[i]-'a'];
        else 
        {
            p = (Tire *)malloc(sizeof(Tire));
        	strcpy(p->word,"#");
            for(j=0;j<26;j++) p->child[j] = NULL;
            
            q->child[s2[i]-'a'] = p;
            q = p;    
        }
    }
    strcpy(q->word,s1);
}
bool find(Tire *q,char *s,int m)
{
    if(m==strlen(s) )
    { 
    //	printf("cmp=%d\n",strcmp(q->word,"#"));
		if(strcmp(q->word,"#")==0) return false;//这里考虑s是字典树中单词的前缀情况
        printf("%s",q->word);
        return true;
    }
    if(q->child[s[m]-'a']) find(q->child[s[m]-'a'],s,m+1);
    else return false;
}

int main()
{
    char s[3003];
    char s1[12],s2[12],temp[12];
    int i,k;
    root = (Tire*)malloc(sizeof(Tire));
    for(i=0;i<26;i++) root->child[i]=NULL;
    
    gets(s1);//START 
    while(scanf("%s",s1),s1[0]!='E')
    {
        scanf("%s",s2);
        insert(s1,s2);    
    }
    getchar();//回车 
    gets(s);//STARAT

    while(gets(s) ,s[0]!='E')
    {
        for(i=0;i<strlen(s);i++)
        {
            if(s[i]==' ') //处理空格 
            {
                printf(" ");
                continue;
            }
            k=0;
            if(s[i]>='a'&&s[i]<='z')//单词 
            {
                while(s[i]>='a'&&s[i]<='z')
                {
                    temp[k++] = s[i++];
                }
                temp[k]='\0';i--;
                //    printf("*%s*",temp);
                if(!find(root,temp,0)) printf("%s",temp);
            }
            else if(s[i]=='\t')     printf("\t");
     		else  printf("%c",s[i]);  //其他字符 
        }
        puts(""); 
    }

    return 0;
}