集训队专题(1)1001 Immediate Decodability

Immediate Decodability

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 29   Accepted Submission(s) : 24
Problem Description
An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.

Examples: Assume an alphabet that has symbols {A, B, C, D}

The following code is immediately decodable:
A:01 B:10 C:0010 D:0000

but this one is not:
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)
 

Input
Write a program that accepts as input a series of groups of records from input. Each record in a group contains a collection of zeroes and ones representing a binary code for a different symbol. Each group is followed by a single separator record containing a single 9; the separator records are not part of the group. Each group is independent of other groups; the codes in one group are not related to codes in any other group (that is, each group is to be processed independently).
 

Output
For each group, your program should determine whether the codes in that group are immediately decodable, and should print a single output line giving the group number and stating whether the group is, or is not, immediately decodable.
 

Sample Input
   
   
   
   
01 10 0010 0000 9 01 10 010 0000 9
 

Sample Output
   
   
   
   
Set 1 is immediately decodable Set 2 is not immediately decodable
 

Source
Pacific Northwest 1998
 

基础字典树,题目大意为输入几组数,当输到“9”时,停止输入,判断在输入的数据中,有没有一个数是另一个数的前缀,如果有就输出“Set # is not immediately decodable”,否则输出“Set # is immediately decodable”,算是比较基础的字典树,由于内存较大,在每组数据后记得把之前的内存以递归的形式删掉即可。

#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
struct TreeNode
{
	int n;
	TreeNode *next[2];
	TreeNode()
	{
		for(int i=0; i<2; i++)
		{
			next[i] = NULL;
		}
		n=1;
	}
};
void insert(char str[],TreeNode *pHead)
{
	TreeNode *p = pHead;
	int nLen=strlen(str);
	for(int i=0; i<nLen; i++)
	{
		if(p->next[str[i]-'0'] == NULL)
			p->next[str[i]-'0'] = new TreeNode;
		else
			p->next[str[i]-'0']->n++;
		p = p->next[str[i]-'0'];
	}
}
int search(char str[],TreeNode *pHead)
{
	int nLen = strlen(str);
	TreeNode *p=pHead;
	bool bfind = false;
	for(int i=0; i<nLen; i++)
		p = p->next[str[i]-'0'];
	return p->n;
}
void Delete(TreeNode *pHead)
{
	for(int i=0; i<2; i++)
	{
		if(pHead != NULL)
		{
			pHead = pHead->next[i];
			Delete(pHead);
		}
	}
	delete pHead;
}
int main()
{
	char str[10][15];
	int nCase=0;
	int n=-1;
	TreeNode *pHead = new TreeNode;
	int flag=0;
	while(scanf("%s",str[++n])!=EOF)
	{
		if(str[n][0] == '9')
		{
			++nCase;
			for(int i=0; i<n; i++)
			{
				if(search(str[i],pHead) > 1)
				{
					printf("Set %d is not immediately decodable\n",nCase);
					break;
				}
				if(i == n-1)
				{
					printf("Set %d is immediately decodable\n",nCase);
				}
			}
			Delete(pHead);
			pHead = new TreeNode;
			n = -1;
		}
		else
		{
			insert(str[n],pHead);
		}
	}
	return 0;
}


你可能感兴趣的:(集训队专题(1)1001 Immediate Decodability)