UVA 644-Immediate Decodability

UVA 644-Immediate Decodability

题目大意:判断字符串组间是否有某个字符串是另一个的前缀

解题思路:利用strcpy,strcat,strcmp用长度小的字符串覆盖掉长的前面部分,然后比较

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main() {
    char a[20][20];
    char d[20];
    int b[20];
    int x = 0;
    while(gets(d) && a[0][0] != EOF) {
        x++;
        int i;
        memset(a, '\0', sizeof(a));
        memset(a, 0, sizeof(a));
        strcpy(a[0],d);
        for(i = 1; gets(a[i]) && a[i][0] != '9'; )
            i++;
        int s = 1;
        for(int j = 0; j < i; j++)
            for(int k = 0; k < i; k++) {
                if(strlen(a[j]) < strlen(a[k])) {
                    char c[20];
                    memset(c, '\0', sizeof(c));
                    strcpy(c, a[j]);
                    strcat(c, a[k]+strlen(a[j]));
                    if(strcmp(c, a[k]) == 0) {
                        s = 0;
                        j = i;
                        k = i;
                    }
                }

            }

        if(s == 0)
            printf("Set %d is not immediately decodable\n", x);
        else if(s == 1)
            printf("Set %d is immediately decodable\n", x);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值