给定一组二进制串,判断是否其中有一串是另外某一串的前缀。对于两个相同的串,我们规定两串相互不为前缀。
例如:10, 010, 0100, 0010, 1010
其中,10是1010的前缀
输入格式
输入有多组数据,每一组数据的第一行为一个整数N,接下来为N个二进制串,每一个串一行。每个二进制串最少有一个比特,而最多有10个比特。每两组输入间有一行空格。N=0时程序结束。
输出格式
对于一组数据,若有某串为另一串的前缀则输出Exist!,否则输出None!
样例输入
5 10 010 0100 0010 1010 4 00 01 10 11 0
样例输出
Exist! None!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char str[11];
string s[1025];
int cmp (char *a, char *b){
return strcmp(a,b);
}
int main(){
int N,i,j;
while (scanf("%d", &N) == 1 && N > 0) {
for (i = 0; i < N; i++){
scanf("%s", str);
s[i] = str;
}
sort(s, s + N);
for (i = 1; i < N; i++) {
j = i - 1;
if (strncmp(s[i].c_str(),s[j].c_str(),min(s[i].size(),s[j].size()))==0&&s[i].size()!=s[j].size())
break;
}
if(i==N)
printf("None!\n");
else
printf("Exist!\n");
}
return 0;
}