UVa_644 - Immediate Decodability

简单模拟题,依次读入编码,比较是否一编码是另一前缀即可。

有两种比较方式,

(1)把所有编码全读入后,再进行判断;

(2)边读入边比较,同时判断

二者比较次数相同,时间复杂度相同,看个人喜好。

代码如下:

#include<stdio.h>
#include<string.h>
using namespace std;

char str[8][11];
int main()
{
    int i=0,j,k,cases=0;//i表示读入二维数组str的下标,比如i=0,str[0]表示读入第一个编码;cases表示第几个测试样例
    bool flag=true,end=false;//flag标志开始is immediately decodable,end=false标志未读到9,end=true标志读到9

    while(scanf("%s",str[i])!=EOF)
    {
        if(str[i][0]!='9') i++;
        else {i=0;cases++;end=true;} //i重置为初值

        if(i==1) continue;//只读入一行,继续读下一行
        else  //
        {
            for(j=0;j<i-1;j++){
                int len=(strlen(str[i-1])<strlen(str[j]))?strlen(str[i-1]):strlen(str[j]);
                for(k=0;k<len;k++){
                    if(str[j][k]==str[i-1][k]) continue;
                    else break;
                }
                if(k>=len) { flag=false; break;} //找到某一编码是另一编码前缀
                else continue;//继续
            }
        }
        if(flag&&end) {
            flag=true,end=false;//重置初值
            printf("Set %d is immediately decodable\n",cases);
        }
        else if(!flag&&end) {
            flag=true,end=false;
            printf("Set %d is not immediately decodable\n",cases);
        }
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值