题目大意:给出几个字符串,判断互相之间是否是前缀。
解题思路:只要与后面的串相比,从 0 开始,边界是较短的字符串,若完全相同则是前缀。
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char code[10000][10000];
char s[10000];
int count = 0;
int main() {
while(scanf("%s", s) != EOF) {
int tot = 0;
while (strcmp(s,"9") != 0) {
strcpy(code[tot++],s);
scanf("%s", s);
}
int tag = 1;
for (int t = 0; t < tot; t++)
for (int n = t+1; n < tot; n++){
int l1 = strlen(code[t]);
int l2 = strlen(code[n]);
for (int i = 0; i < min(l1,l2); i++){
if (code[t][i] != code[n][i])
break;
if (i == min(l1,l2)-1)tag = 0;
}
}
if(tag) printf("Set %d is immediately decodable\n", ++count);
else printf("Set %d is not immediately decodable\n", ++count);
}
return 0;
}