水题一道排个序从头扫到尾
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int N=1;
struct Str
{
char s[15];
int len;
Str(){memset(s,0,sizeof(s));len = 0;}
};
int cmp(struct Str a, struct Str b)
{
if(a.len < b.len)return 1;
else if(a.len > b.len) return 0;
}
int Match(struct Str a, struct Str b)
{
for(int i = 0; i < a.len; i++)
if(a.s[i]!=b.s[i])return 0;//a不是b的前缀
return 1;//a是b的前缀
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
struct Str str[9];
int T = 0;
while(gets(str[T++].s)!=NULL)
{
str[T-1].len = strlen(str[T-1].s);
if(str[T-1].s[0] == '9')
{
T--;
sort(str,str + T, cmp);
int flag = 1;
for(int i = 0; i < T-1&&flag; i++)
{
for(int j = i+1 ; j < T&&flag; j++)
if(Match(str[i],str[j]))flag = 0;//长度相等其实可以不用判断因为题目说了不会有两个相等的
}
if(flag) printf("Set %d is immediately decodable\n", N++);
else
printf("Set %d is not immediately decodable\n", N++);
T = 0;
}
}
return 0;
}