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;
}