zoukankan      html  css  js  c++  java
  • HDOJ-1075 What Are You Talking About 字典树

    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(' '), enter(' ') 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]='';
        }
    };f s [1000000];
    int cut=0;
    void charu(char *s1,char *s2)
    {
        int i,k=0,su;
        for (i=0;s2[i]!='';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]!=''&&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]=='') break;
            z=0;
            for (i=0;str1[i]!='';i++)
            {
                if (islower(str1[i]))
                {
                    str2[z]=str1[i];
                    z++;
                }
                else
                {
                    str2[z]='';
                    if (find(str2)) printf("%s",str2);
                    printf("%c",str1[i]);
                    z=0;
                }
            }
            if (z!=0)
            {
                  str2[z]='';
                    if (find(str2)) printf("%s",str2);
                    z=0;
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    hdu 5059 判断数字表示方式以及范围合法(int型之内)
    hdu1024 最大m子序列和
    POJ 1573 Robot Motion 模拟 难度:0
    POJ 2632 Crashing Robots 模拟 难度:0
    POJ 1068 Parencodings 模拟 难度:0
    ncs安装及初次运行
    POJ 1328 Radar Installation 贪心 难度:1
    POJ 3094 Quicksum 难度:0
    django.test.client 使用随记
    POJ 2255 Tree Recovery 树的遍历,分治 难度:0
  • 原文地址:https://www.cnblogs.com/pblr/p/4712712.html
Copyright © 2011-2022 走看看