码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ-1075 What Are You Talking About 字典树

时间:2015-08-08 11:48:38      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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!
 
     字典树:先以给的火星文单词建树,并把对应的英文记录在树的末尾。查找时应整个单词的查找。
 
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
struct f
{
    int next[30];
    int v;
    char englishi[15];
    void ff()
    {
        memset(next,-1,sizeof(next));
        v=0;
        englishi[0]=‘\0‘;
    }
};f s [1000000];
int cut=0;
void charu(char *s1,char *s2)
{
    int i,k=0,su;
    for (i=0;s2[i]!=‘\0‘;i++)
    {
        su=s2[i]-‘a‘;
        if (s[k].next[su]==-1)
        {
            cut++;
            s[k].next[su]=cut;
            s[cut].ff();
        }
        k=s[k].next[su];
    }
    s[k].v=1;
    strcpy(s[k].englishi,s1);
    return ;
}
int find(char *s1)
{
    int i,k=0,su;
    for (i=0;i<strlen(s1);i++)
    {
        su=s1[i]-‘a‘;
        if (s[k].next[su]==-1) break;
        k=s[k].next[su];
    }
    if (s[k].v&&s[k].englishi[0]!=‘\0‘&&i==strlen(s1))
    {
        printf("%s",s[k].englishi);
        return 0;
    }
    return 1;
}
int main()
{
    char str1[3005],str2[3005];
    int z,i;
    s[cut].ff();
    gets(str1);
    while (~scanf("%s",&str1))
    {
        if (strcmp("END",str1)==0) break;
        scanf("%s",&str2);
        charu(str1,str2);
    }
    getchar();
    gets(str1);
    while (1)
    {
        gets(str1);
        if (str1[0]==‘E‘&&str1[1]==‘N‘&&str1[2]==‘D‘&&str1[3]==‘\0‘) break;
        z=0;
        for (i=0;str1[i]!=‘\0‘;i++)
        {
            if (islower(str1[i]))
            {
                str2[z]=str1[i];
                z++;
            }
            else
            {
                str2[z]=‘\0‘;
                if (find(str2)) printf("%s",str2);
                printf("%c",str1[i]);
                z=0;
            }
        }
        if (z!=0)
        {
              str2[z]=‘\0‘;
                if (find(str2)) printf("%s",str2);
                z=0;
        }
        printf("\n");
    }
    return 0;
}

 

HDOJ-1075 What Are You Talking About 字典树

标签:

原文地址:http://www.cnblogs.com/pblr/p/4712712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!