AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: String
// 644 - Immediate Decodability
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
int cmp_len(const void* _a, const void* _b)
{
char* a = (char*)_a;
char* b = (char*)_b;
return strlen(a) - strlen(b);
}
int main(void)
{
char s[8][10];
int i, j, n, ok, flag, count=1;
while(1)
{
n = 0;
while(ok=scanf("%s", s[n])) // 检测文件结尾
{
if(s[n][0] == '9')
break;
else
n++;
}
if(ok == -1) break;
qsort(s, n, sizeof(s[0]), cmp_len);
flag = 1;
for(i=0; i<n && flag; i++)
for(j=i+1; j<n && flag; j++)
if(strncmp(s[i], s[j], strlen(s[i])) == 0)
flag = 0;
if(i == n)
cout << "Set " << count << " is immediately decodable" << endl;
else
cout << "Set " << count << " is not immediately decodable" << endl;
count++;
}
return 0;
}