【字典树】Immediate Decodability

14 篇文章 0 订阅
14 篇文章 1 订阅

1004: Immediate DecodabilityTime
给出一些数字串,判断是否有一个数字串是另一个串的前缀。
Input输入数据为多组数据,每组数据读到 9 时结束。
数字串只包含 0,1,记每个数字串长度为 l,则 1≤l≤10。每组数据至少有 2 个数字串,至多有 8 个数字串。
Output
对于每组数据,如果不存在一个数字串是另一个串的前缀,输出一行 Set t is immediately decodable ,否则输出一行 Set t is not immediately decodable ,其中 t 是这一组数据的组号。
Sample Input
01
10
0010
0000
9
01
10
010
0000
9
Sample Output
Set 1 is immediately decodable
Set 2 is not immediately decodable

很简单的一道字典树模板题,主要是熟悉字典树的使用方法。
利用数组ch存放各个结点,bo数组用于标记和判断当前结点是否已有,每次插入字符串的同时判断该字符串是否有子串或者为前面字符串的子串(分别对应代码20行和22行),运算效率极高。

#include<bits/stdc++.h>
using namespace std;
const int N = (int) (1e5+10),Z=10;
int T,n,tot;int coun=0;
int ch[N][Z];
bool bo[N];
void clean() {
    memset(ch,0,sizeof ch);
    memset(bo,0,sizeof bo);
}
bool Insert(string s)
{
    int len=s.length();
    int u=1;
    bool flag=false;
    for(int i=0;i<len;i++) {
        int c=s[i]-'0';
        if(ch[u][c]==0)
            ch[u][c]=++tot;
        else if(i==len-1)
            flag=true;u=ch[u][c];
        if(bo[u])flag=true;
    }
    bo[u]=true;
    return flag;
}
int main()
{
    string s;
    while(cin>>s) {
        clean();coun++;tot=1;
        bool ans=false;
        string s;
        while(cin>>s&&s!="9") {
            if(Insert(s))ans=true;
        }
        if(ans)printf("Set %d is not immediately decodable\n",coun);
        else printf("Set %d is immediately decodable\n",coun);
    }
    return 0;
}
```c
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值