zoukankan      html  css  js  c++  java
  • POJ 2503 Babelfish(字典树模板题)

    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.

    题意:输入几个串建立字典,空格前的串表示字典中被查询的串,空格后的串表示前串对应的串。建立完词典后,输入几组串,在字典树中查询,如果存在,输出存在的串所对应的后串,若查找不到输出eh。

    标准的模板题,后面注明输入量很大是真的,因为我字典树查找到串之后再用for遍历找对应的后串就超时了,偷懒用了个map直接对应查找串2。

    代码如下:

    #include<stdio.h>//数组模拟
    #include<string.h>
    #include<map>
    #include<string>
    using namespace std;
    int tot;//为每个不相同的字符标号
    int trie[1000000][26];//有那么多层的树,每层有26个分支
    bool isw[1000000];//用于标记是否是单词完结节点
    struct str//第一个字符串和第二个字符串相匹配
    {
        char str1[18];
        char str2[18];
    } a[100005];
    int insert(char *str,int rt)//建树
    {
        for(int j=0; str[j]; j++)
        {
            int x=str[j]-'a';//第x个分支是这个字母,此层的分支就是x,从x再分出节点
            if(trie[rt][x]==0)
            {
                trie[rt][x]=++tot;//若是未标记的新字母,则重新标号
            }
            rt=trie[rt][x];//用rt记录这个新号数,然后从这个号数开始找下一个字母的位置
        }
        isw[rt]=true;//标记为结束
    }
    bool find(char *str,int rt)//查询字典
    {
        for(int i=0; str[i]; i++)
        {
            int x=str[i]-'a';
            if(trie[rt][x]==0)//若被查询的字母不存在标记,则不是字典中的数
                return false;
            rt=trie[rt][x];//若找到了这个字母,记录其所在节点,一直重走一遍这个单词的每一个字母,直到完全相符,并返回结束标记节点
        }
        return isw[rt];
    }
    
    int main()
    {
        map<string,int>mp;
        mp.clear();
        tot=0;///根节点
        int floor=++tot;
        int i=1;
        memset(isw,false,sizeof(isw));
        memset(trie[floor],0,sizeof(trie[floor]));
        char tmp;
        while(tmp=getchar())//输入时用tmp承接一个字符,看看是否是
    
        {
            if(tmp=='
    ')//若等于换行符,则打破输入字典循环,开始询问输入
                break;
            a[i].str1[0]=tmp;//若不为换行符,将第一个输入的字符放入字符串str1中
            scanf("%s %s",a[i].str1+1,a[i].str2);//并且从str1+1处继续输入,并输入str2
            getchar();//最后把回车确认的字符用getchar()带走
            insert(a[i].str2,floor);//建树
            mp[a[i].str2]=i;
            i++;//输入下一个字符串
        }
        char s[15];
        while(scanf("%s",s)!=EOF)
        {
            if(find(s,floor))//查找树
                printf("%s
    ",a[mp[s]].str1);
            else printf("eh
    ");
        }
    }
    
  • 相关阅读:
    浮点型float
    Linux时间同步命令
    四层负载均衡和七层负载均衡的区别
    Linux下把Mysql和Apache加入到系统服务里
    Linux下Nagios的安装与配置
    Linux是怎么启动的(整理)
    centos 双网卡 eth0 eth1 配置
    MYSQL的常用命令和增删改查语句和数据类型
    CentOS 下如何修改 MySQL 的密码
    mysql unrecognized service问题解决
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135862.html
Copyright © 2011-2022 走看看