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;
    }
    
  • 相关阅读:
    王爽《汇编语言》第三版 第一章 基础知识
    JAVA拷贝视频文件无法播放的解决方法
    算法导论 第2章 算法基础
    今天来个爪哇去边框的小代码
    设计模式_Adapt
    qs面试记录
    zl面试记录
    pa_ns2面试记录
    pa_ns1面试总结
    sfjc面试记录
  • 原文地址:https://www.cnblogs.com/pblr/p/4712712.html
Copyright © 2011-2022 走看看