杭电acm 1305Immediate Decodability(字典树)
2017-08-14 15:08
169 查看
Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3590 Accepted Submission(s): 1865
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
想法:
字典树模板
代码:
#include<stdio.h>
#include<string.h>
const int N=2;
struct Trie
{
int num;
Trie *nx
;
};
Trie *root;
void init (Trie *t)
{
for (int j = 0; j < N; ++j) //初始化
{
t->nx[j] = NULL;
}
t->num = 0;
}
void Insert (char str[])//插入单词
{
Trie *p = root;
int len = strlen (str);
for (int i = 0; i < len; ++i)
{
int id = str[i] - '0';
if (p->nx[id] == NULL)
{
Trie *t = new Trie;
init(t);
p->nx[id] = t;
}
p = p->nx[id];
p->num++;
}
}
int Find(char str[])//查找前缀为str的单词数
{
Trie *p = root;
int cnt,len = strlen(str);
for (int i = 0;i < len;++i)
{
int id = str[i] - '0';
if (p->nx[id] == NULL) return 0;
p = p->nx[id];
cnt = p->num;
}
return cnt;
}
void Delete(Trie *t)
{
for (int i = 0;i < N;++i)
{
if (t->nx[i]!=NULL) Delete(t->nx[i]);
}
delete t;
}
int main()
{
int kas=0;
root = new Trie;
init(root);
int kk=1;
char s[50][50];
while(gets(s[++kas]))
{
if(s[kas][0]=='9')
{
int i,flag=1;
for(i=1;i<kas;i++)
{
if(Find(s[i])>=2)
{
flag=0;
break;
}
}
if(flag) printf("Set %d is immediately decodable\n",kk++);
else printf("Set %d is not immediately decodable\n",kk++);
kas=0;
root = new Trie;
init(root);
}
else
{
Insert(s[kas]);
}
}
}
相关文章推荐
- 杭电ACM1671——Phone List~~字典树
- 杭电acm 5687Problem C(字典树)
- 杭电acm 1075What Are You Talking About(字典树)
- 杭电acm 1671Phone List(字典树)
- 杭电ACM 2014:青年歌手大奖赛_评委会打分
- 杭电ACM 2006: 求奇数的乘积
- 奋战杭电ACM(DAY11)1017
- 杭电ACM 2019:数列有序!
- 奋战杭电ACM(DAY13)1021
- 杭电ACM2016首字母变大写----20140726
- 杭电ACM 2041超级楼梯--&&2047阿牛的EOF牛肉串------20140728
- 杭电ACM1166——敌兵布阵~~线段树
- 杭电ACM1390——Binary Numbers
- 杭电acm 2021 发工资咯:)
- 杭电acm2089 hdu-acm-2089不要62解题报告
- 杭电ACM1301——Jungle Roads~~最小生成树
- 杭电_ACM_Fibonacci
- acm-杭电-5645
- 杭电acm足迹
- 【ACM】杭电1022:Train Problem I