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