zoukankan      html  css  js  c++  java
  • trie树 POJ 2503 Babelfish

    Description

    You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
    Input

    Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
    Output

    Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.
    Sample Input

    dog ogday
    cat atcay
    pig igpay
    froot ootfray
    loops oopslay

    atcay
    ittenkay
    oopslay
    Sample Output

    cat
    eh
    loops
    Hint

    Huge input and output,scanf and printf are recommended.
    第一次写字典树,之前做一个比较难的半天看不懂。。。找个简单的先熟练一下

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <algorithm>
    using namespace std;
    const int maxnode=1000000+100;
    int ch[maxnode][26];        //ch[i][j]储存的是i节点的字符为j的子节点的位置
    string val[maxnode];        //当前节点的值,中间节点统一为0或忽略
    int sz;
    int idx(char c)
    {
        return c-'a';
    }
    void Insert(char *s1,string s2)
    {
        int u=0,n=strlen(s1);
        for(int i=0;i<n;i++)
        {
            int c=idx(s1[i]);
            if(!ch[u][c])      //如果u节点不存在值为c的子节点则新建一个
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                //val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];       //得到子节点的位置
        }
        val[u]=s2;
    }
    string find(string s)
    {
        int u=0,n=s.length();
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(ch[u][c]==0)
                return "eh";
            u=ch[u][c];
        }
        return val[u];
    }
    int main()
    {
        char s[30],tmp1[15],tmp2[15];
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));
        while(gets(s))
        {
            if(strcmp(s,"")==0)
                break;
            int len=strlen(s),flag=0;
            memset(tmp1,0,sizeof(tmp1));
            memset(tmp2,0,sizeof(tmp2));
            int i,j=0;
            for(i=0;i<len;i++)
            {
                if(s[i]==' ')
                {
                    flag=1;
                    j=0;
                    continue;
                }
                if(flag==0)
                    tmp1[j++]=s[i];
                else
                    tmp2[j++]=s[i];
            }
            Insert(tmp2,string(tmp1));
        }
        string temp;
        while(cin>>temp)
            cout<<find(temp)<<endl;
        return 0;
    }
  • 相关阅读:
    第一篇博文
    重拾javascript系列-JS声明详解之var
    重拾Javascript系列
    AtCoder DP Contest 26题
    DP题
    一众数论
    字符编码
    C# DateTime类型和sqlserver DateTime精度不同
    vs2015中的数据库架构对比工具(New Schema Comparison)
    SqlServer常用语句
  • 原文地址:https://www.cnblogs.com/acagain/p/9180731.html
Copyright © 2011-2022 走看看