题目大意:输入一组二进制的字符串,看其中任意一个字符串是否是其他字符串的前缀。
解题思路:逐一对比,遇到符合条件的直接跳出。注意串的比较可能前者是后者前缀,也可能后者是前者前缀。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
char a[15][10];
int n, en = 0, i, j, k, res;
while(scanf("%s",a[0]) != EOF ){
en ++;
for( i = 1 ;a[i-1][0] != '9' && i <15 ; i++)
cin >> a[i];
for( i = 0; a[i][0] != '9' ; i++ )
for( j = i+1; a[j][0] != '9' ; j++) {
res = 0;
if( strlen (a[i]) <= strlen(a[j]) ) {
for( k = 0; k < strlen(a[i]) ; k++) {
if( a[i][k] == a[j][k] ) res++;
if( res == strlen(a[i]) ) goto nott;
}
}
else {
for ( k = 0; k < strlen(a[j]) ; k++)
if( a[i][k] == a[j][k] ) res++;
if( res == strlen(a[j]) ) goto nott;
}
}
cout << "Set " << en << " is immediately decodable" << endl;
continue;
nott:cout << "Set " <<en << " is not immediately decodable" << endl;
}
return 0;
}