UVA-644 Immediate Decodability

2016-07-12

UVA - 644 Immediate Decodability

题目大意:给出几个字符串,判断互相之间是否是前缀。

解题思路:只要与后面的串相比,从 0 开始,边界是较短的字符串,若完全相同则是前缀。

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char a[100];
    char str[100][100];
    int count = 0;
    while ( cin >> a ) {
        int num = 0;
        while ( strcmp( a , "9" ) != 0 ) {
             strcpy( str[num++] , a );
            cin >> a;
        }
        num--;
        count++;
        int tag = 0;
        int flag = 0;
        for (int i = 0; i <= num; i++) {
            for (int j = 0; j <= num; j++) {
                int temp = 0;
                int l1 = strlen(str[i]);
                int l2 = strlen(str[j]);
                if ( l1 != l2 ) {
                    for (int k = 0; k < min(l1,l2); k++) 
                        if ( str[i][k] == str[j][k] ) {
                            tag = 1;
                            temp++;
                        }
                    if ( tag == 1 && temp == min(l1,l2) ) {
                        flag = 1;
                        break;
                    }
                }
            }
            if ( flag == 1 )    break;
        }
        if ( flag == 1 )    cout << "Set " << count << " is not immediately decodable" << endl;
        else    cout << "Set " << count << " is immediately decodable" << endl; 
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值