644 - Immediate Decodability

题目:644 - Immediate Decodability


题目大意:给出一组二进制数,如果这一组的每一个二进制数都不是其他的任何一个的前缀,则这一组数据是is immediately decodable,否则不是;


解题思路,判断strstr(char * x,char *y)== x? 是则y是x的前缀,否则则不是;注意:每一个二进制数要和其余的二进制数都比较一遍;


#include<stdio.h>
#include<string.h>


const int N = 15;
const int M = 10;
char code[M][N];
int i = 0;
int t = 0;
bool judge() {

	for (int  j = 0; j < i; j++ ) 
		for (int  k = 0;  k < i; k++) 	{			
				
			if(k != j) {
					if( strstr(code[k],code[j]) == code[k] )
						return false;
			}
		}
					return true;
}
int main() {

	while (scanf("%s", code[i]) != EOF) {
		if(strcmp(code[i], "9") != 0) {
			i++;
		}
		else {
			t++;
			if(judge())	
				printf("Set %d is immediately decodable\n",t);
			else
				printf("Set %d is not immediately decodable\n",t);			
			i = 0;
	
		}
	}
	
	
	return 0;
}


你可能感兴趣的:(644 - Immediate Decodability)