POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List
题目来源:http://poj.org/problem?id=1056
http://poj.org/problem?id=3630
两题非常类似,所以在这里一并做了。
1056题目大意:
如果一组编码中不存在一个编码是另一个编码的前缀的情况,我们就称这组编码是“可立即解码的”(immediately decodable)。我们假定编码都是二进制的,一组编码中没有相同的码。每个编码长度都不超过10,每组编码数目都在2到8之间。
比如,一组二进制编码:{A, B, C, D},其中:A: 01 B: 10 C: 0010 D: 0000,这组编码是immediately decodable的;另一组编码:A: 01 B: 10 C: 010 D:0000 则不是immediately decodable的, 因为A是C的前缀。
写一个程序判断一组编码是不是immediately decodable的。
输入:由一组0和1组成的二进制码组成,以一个9表示一组编码的输入结束。
输出:格式见sample。
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
本题数据类型简单,数据规模小,暴力其实就可以过。但是本题如果不用前缀树做就显得没有价值了。
Trie树,又称前缀树或字典树。Trie一词本来源于retrieval,发音[tri],但似乎更多人读为[trai]。看一下维基上的示意图应该很容易理解。

Trie树是一种用于快速检索的多叉树结构,如上图所示的Trie树中,用11个节点保存了8个字符串(to, tea, ted, ten, A, I, in, inn). 如果两个词具有相同前缀,那么从根节点到两词对应的节点的路径在公共前缀部分是重合的。显然这种数据结构非常适合这道题的实现,用一棵二叉trie树即可。每读入一个新的编码便插入树中,如果插入过程中发现当前已存在某编码是该码的前缀或该码是已存在的某码的前缀,侧该组编码不是immediately decodable的。
////////////////////////////////////////////////////////////////////
// POJ1056 IMMEDIATE DECODABILITY
// Memory: 144K Time: 0MS
// Language: C++ Result : Accepted
//////////////////////////////////////////////////////////////////// #include <cstdio>
#include <iostream>
using namespace std; struct Node {
int child[];
int is_leaf;
};
Node tree[];
int tree_pointer = ;
bool flag = true;; bool insertNode (char * s, int index) {
if (tree[index].is_leaf == ) {
return false;
}
if (*s == ) {
if (tree[index].child[] == && tree[index].child[] == ) {
tree[index].is_leaf = ;
return true;
} else {
return false;
}
}
while (*s) {
int d = *s - '';
if (tree[index].child[d] == ) {
tree[index].child[d] = tree_pointer++;
}
return insertNode(s + , tree[index].child[d]);
}
} int main(void) {
char buf[];
int case_no = ;
tree[].is_leaf = ;
while (scanf("%s", buf) != EOF) {
if (buf[] == '') {
if (flag == true) {
printf("Set %d is immediately decodable\n", ++case_no);
} else {
printf("Set %d is not immediately decodable\n", ++case_no);
}
tree_pointer = ;
memset(tree, , sizeof(tree));
flag = true;
continue;
}
if (flag) {
flag = insertNode(buf, );
}
}
}
然后看一下3630.
题目大意:
给出一些电话号码,判断是否存在一个号码是另一个号码前缀的情况, 如果不存在说明这些号码是consistent的。
输入:多个case组成,每个case第一行为号码数n, 接下来的n行每行为一个电话号码。
输出:对于每个case,若是consistent的输出YES, 否则输出NO。
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES
两题几乎是一样的,把上题里的二叉树改为十叉树即可。
//////////////////////////////////////////////////////////////////////////
// POJ3630 Phone List
// Memory: 2992K Time: 157MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <cstdio>
#include <iostream>
using namespace std; struct Node {
int child[];
int flag; //0 非叶子节点;1 未标记的叶子节点;2 标记的叶子节点
};
Node tree[ << ];
int tree_pointer = ;
bool flag = true;; bool insertNode (char * s, int index) {
if (tree[index].flag == ) {
return false;
}
if (*s == ) {
if (tree[index].flag == ) {
tree[index].flag = ;
return true;
} else {
return false;
}
}
while (*s) {
int d = *s - '';
if (tree[index].child[d] == ) {
tree[index].child[d] = tree_pointer++;
tree[tree_pointer - ].flag = ;
}
tree[index].flag = ;
return insertNode(s + , tree[index].child[d]);
}
} int main(void) {
char buf[];
int case_num;
scanf("%d", &case_num);
for (int case_no = ; case_no < case_num; ++case_no) {
int n;
tree_pointer = ;
memset(tree, , sizeof(tree));
flag = true;
scanf("%d", &n);
for (int num_id = ; num_id < n; ++num_id) {
scanf("%s", buf);
if (flag) {
flag = insertNode(buf, );
}
}
if (flag == true) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List的更多相关文章
- POJ--1056 IMMEDIATE DECODABILITY && POJ--3630 Phone List(字典树)
题目链接 题目大意 看输入的每个字符串中是否有一个字符串是另一个字符串的前缀 #include<iostream> #include<cstring> #include< ...
- POJ1056 IMMEDIATE DECODABILITY【数据结构】
题目地址:http://poj.org/problem?id=1056 Description An encoding of a set of symbols is said to be immedi ...
- UVa 644 Immediate Decodability
吐槽下我的渣渣英语啊,即使叫谷歌翻译也没有看懂,最后还是自己读了好几遍题才读懂. 题目大意:题意很简单,就是给一些互不相同的由'0','1'组成的字符串,看看有没有一个字符串是否会成为另一个的开头的子 ...
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- UVA644 Immediate Decodability
UVA644 Immediate Decodability Trie Trie模板题 难度几乎相等的题P2580 于是他错误的点名开始了 对于每组数据都清空树太浪费时间,所以我们只要在需要新点时预先把 ...
- HDU 1305 Immediate Decodability 可直接解码吗?
题意:一个码如果是另一个码的前缀,则 is not immediately decodable,不可直接解码,也就是给一串二进制数字给你,你不能对其解码,因解码出来可能有多种情况. 思路:将每个码按长 ...
- 「UVA644」 Immediate Decodability(Trie
题意翻译 本题有多组数据.每组数据给出一列以"9"结尾的仅包含'0'和'1'的字符串,如果里面有一个是另一个的子串,输出"Set &case is not imm ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
随机推荐
- 类型:.net;问题:C#lambda表达式;结果:Lambda表达式详解
Lambda表达式详解 前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1 ...
- Access restriction required library rt.jar
在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...
- C++知识点总结(四)——面向对象的编程细节总结
1.空类的默认函数 一般情况下,对于任意一个类A,如果程序员不显示的声明和定义上述函数,C++编译器将会自动的为A产生4个public inline(公有.内联)的默认函数,这4个函数最常见的形式为: ...
- 【总结整理】AMAP学习AMAP.PlaceSearch()
http://lbs.amap.com/api/javascript-api/reference/search#m_AMap.PlaceSearch http://lbs.amap.com/api/j ...
- 【总结整理】JS的继承
参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/inheritance.html#toc4 function Shape() { this.x = 0; t ...
- HDU 6395(2018多校第7场1010)Sequence
不久前做过POJ3070,所以知道这题要用矩阵快速幂优化,但是这个题的递推公式中有一项⌊p/n⌋,场上就不会了... 下来才知道要用分块矩阵快速幂,因为⌊p/n⌋最多有2√p块,可以对每一块使用快速幂 ...
- 使用bat一键打开java、jar、py文件
直接运行jar是没有命令行窗口的,如果想有命令行窗口,一般来说是要 win+r 运行cmd,定位到所在目录,然后用命令行执行 java -jar xxx.jar 而对于python,打开py文件也是麻 ...
- 完全离线安装VSCode插件--Eslint
最近折腾了一番,总算把Eslint插件在离线的情况下安装好了.之前查了挺多,但是很多方法还是在没有完全离线的情况下进行的.之所以想完全离线安装,主要是因为我们工作的地方是禁止访问外网的,所以像直接执行 ...
- springcloud安全控制token的创建与解析
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorith ...
- C#中的多线程 - 高级多线程
1非阻塞同步Permalink 之前,我们描述了即使是很简单的赋值或更新一个字段也需要同步.尽管锁总能满足这个需求,一个存在竞争的锁意味着肯定有线程会被阻塞,就会导致由上下文切换和调度的延迟带来的开销 ...