zoukankan      html  css  js  c++  java
  • Trie树(c++)

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    int ch[10000][100];
    int i,j,k;
    int cnt=0;
    int main()
    {
        int n,m;
        cin>>n>>m;
        int pos=1;
        for(i=1;i<=n;i++)
        {
            char s[10000];
            cin>>s;
            int c=0;
            int h1=strlen(s);
            for(j=0;j<h1;j++)
            {
                int hh=s[j]-'0';
                if(ch[c][hh]==0) 
                {
                    ch[c][hh]=pos;//编号 
                    pos++;
                }
                c=ch[c][hh];//向下排 
            }
        }
        for(i=1;i<=m;i++)
        {
            char a[10000];
            cin>>a;
            int h1=strlen(a);
            int hh=0;
            int f=1;
            for(j=0;j<h1;j++)
            {
                int h2=a[j]-'0';
                if(ch[hh][h2]!=0)
                {
                    hh=ch[hh][h2];
                }
                else 
                {
                    cout<<"No"<<endl;
                    f=0;
                    break;
                }
            }
            if(f==1) cout<<"Yes"<<endl;
        }
        return 0;
    }

    将字符组成一棵树,相同前缀的合并。

    统计难题

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 46620    Accepted Submission(s): 16559

    Problem Description
    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
     
    Input
    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
    注意:本题只有一组测试数据,处理到文件结束.
     
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
    5 4
    banana
    band
    bee
    absolute
    acm
    ba
    b
    band
    abc
     
    Sample Output
    2
    3
    1
    0
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    int ch[10000][100];
    int i,j,k;
    int cnt[10000];
    int main()
    {
        int n,m;
        cin>>n>>m;
        int pos=1;
        for(i=1;i<=n;i++)
        {
            char s[10000];
            cin>>s;
            int c=0;
            int h1=strlen(s);
            for(j=0;j<h1;j++)
            {
                int hh=s[j]-'0';
                if(ch[c][hh]==0) 
                {
                    ch[c][hh]=pos;//编号
                    pos++;
                }
                cnt[ch[c][hh]]++;
                c=ch[c][hh];//向下排 
                
            }
        }
        for(i=1;i<=m;i++)
        {
            char a[10000];
            cin>>a;
            int h1=strlen(a);
            int hh=0;
            int f=1;
            for(j=0;j<h1;j++)
            {
                int h2=a[j]-'0';
                if(ch[hh][h2]==0) 
                {
                    cout<<0<<endl;
                    break;
                }
                if(ch[hh][h2]!=0)
                {
                    if(j==h1-1) 
                    {
                        cout<<cnt[ch[hh][h2]]<<endl;
                        break;
                    }
                    hh=ch[hh][h2];
                }
                
            }
        }
        return 0;
    }

    简单的加一个count数组。记录每个点下面有几个分支(包含此点)。

    Immediate Decodability

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3774    Accepted Submission(s): 1979

    Problem Description
    An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.
    Examples: Assume an alphabet that has symbols {A, B, C, D}
    The following code is immediately decodable: A:01 B:10 C:0010 D:0000
    but this one is not: A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)
     
    Input
    Write a program that accepts as input a series of groups of records from input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently).
     
    Output
    For each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable.
     
    Sample Input
    2
    5
    01
    10
    0010
    0000
    9
    5
    01
    10
    010
    0000
    9
     
    Sample Output
    Set 1 is immediately decodable
    Set 2 is not immediately decodable
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    int ch[10000][100];
    int i,j,k;
    int cnt[10000];
    int book[10000];
    int main()
    {
        int s;
        cin>>s;
        while(s--)
        {
            int n;
            cin>>n;
            int pos=1;
            for(i=1;i<=n;i++)
            {
                char s[10000];
                cin>>s;
                int c=0;
                int h1=strlen(s);
                for(j=0;j<h1;j++)
                {
                    int hh=s[j]-'0';
                    if(ch[c][hh]==0) 
                    {
                        ch[c][hh]=pos;//编号
                        pos++;
                    }
                    cnt[ch[c][hh]]++;
                    if(j==h1-1) book[i]=ch[c][hh];
                    c=ch[c][hh];//向下排 
                    
                }
            }
            int f=0;
            for(i=1;i<=n;i++)
            {
                if(cnt[book[i]]>1)
                {
                    cout<<"No"<<endl;
                    f=1;
                    break;
                }
            }
            if(f==0) cout<<"Yes"<<endl;
        }
        return 0;
    }

    判断以每个串为前缀的是否唯一。

    代码输入输出并不完全符合题意,请见谅。

    蒟蒻总是更懂你✿✿ヽ(°▽°)ノ✿
  • 相关阅读:
    ubuntun16.04不支持intel的最新网卡,升级到17.10后解决
    python网络爬虫之使用scrapy下载文件
    Django之QuerySet 创建对象
    一起来学linux:磁盘与文件系统:
    python自动化运维九:Playbook
    一起来学linux:FTP服务器搭建
    Learning Scrapy 中文版翻译 第二章
    python自动化运维八:Ansible
    linux c编程:make编译一
    【原创】大叔经验分享(27)linux服务器升级glibc故障恢复
  • 原文地址:https://www.cnblogs.com/WWHHTT/p/7989935.html
Copyright © 2011-2022 走看看