POJ 1056 IMMEDIATE DECODABILITY
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9630 | Accepted: 4555 |
Description
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
Output
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
题目大意:给定一段编码,每段编码以“9”结束,判断是否有一个编码是另一个编码的前缀。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std; typedef struct node
{
int n;
node *next[];
node()
{
for (int i = ; i < ; i++)
{
next[i] = NULL;
}
n = ;
}
}TreeNode; void Insert(char str[], TreeNode *pHead)
{
TreeNode *p = pHead;
int nLen = strlen(str);
for (int i = ; i < nLen; i++)
{
if (p->next[str[i] - ''] == NULL)
{
p->next[str[i] - ''] = new TreeNode;
}
else
{
p->next[str[i] - '']->n++;
}
p = p->next[str[i] - ''];
}
} int Search(char str[], TreeNode *pHead)
{
int nLen = strlen(str);
TreeNode *p = pHead;
bool bfind = false;
for (int i = ; i < nLen; i++)
{
p = p->next[str[i] - ''];
}
return p->n;
} void Delete(TreeNode *pHead)
{
for (int i = ; i < ; i++)
{
if (pHead != NULL)
{
pHead = pHead->next[i];
Delete(pHead);
}
}
delete pHead;
} int main()
{
char str[][];
int nCase = ;
int n = -;
TreeNode *pHead = new TreeNode;
int flag = ;
while(scanf("%s", str[++n]) != EOF)
{
if (str[n][] == '')
{
++nCase;
for (int i = ; i < n ; i++)
{
if (Search(str[i], pHead) > )
{
printf("Set %d is not immediately decodable\n", nCase);
break;
}
if (i == n - )
{
printf("Set %d is immediately decodable\n", nCase);
}
}
Delete(pHead);
pHead = new TreeNode;
n = -;
}
else
{
Insert(str[n], pHead);
}
}
return ;
}
POJ 1056 IMMEDIATE DECODABILITY的更多相关文章
- poj 1056 IMMEDIATE DECODABILITY(KMP)
题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 1056 IMMEDIATE DECODABILITY 【Trie树】
<题目链接> 题目大意:给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀. 解题分析: Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节 ...
- POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找
POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...
- 【POJ】1056 IMMEDIATE DECODABILITY
字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...
- 1056 IMMEDIATE DECODABILITY
题目链接: http://poj.org/problem?id=1056 题意: 给定编码集, 判断它是否为可解码(没有任何一个编码是其他编码的前缀). 分析: 简单题目, 遍历一遍即可, 只需判断两 ...
- POJ 1056
#include <iostream> #include <string> #define MAXN 50 using namespace std; struct node { ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- 蓝书2.3 Trie字典树
T1 IMMEDIATE DECODABILITY poj 1056 题目大意: 一些数字串 求是否存在一个串是另一个串的前缀 思路: 对于所有串经过的点权+1 如果一个点的end被访问过或经过一个被 ...
随机推荐
- IE下的圆角
元素{ position: relative;/*必须*/ z-index: 10;/*必须*/ border-radius: 8px; -moz-border-radius: 8px; -webki ...
- viewpager的使用-新方法 5.1
效果图: 添加依赖包: compile ‘com.android.support:design:22.2.0‘ 布局文件: <?xml version="1.0" encod ...
- C# 操作字符串,在某些特定的字符后面或前面添加其它字符
C# 操作字符串,在某些特定的字符后面或前面添加其它字符,解决方法: 字符串替换或正则表达式替换即可. 示例:实现的是在每个“第”前面添加一个逗号,在每个“方案”后面添加一个冒号. string s ...
- JS 字符串 时间 数字函数操作 事件
字符串 操作 var s="abcdefg" s.tolowerCase() 转小写 s.toupperCase() 转大写 s.substring(2,5) 索引下 ...
- maven打包错误:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] Scanning for projects...[INFO] ...
- 使用JDK自带的jmap和jhat监控处于运行状态的Java进程
对于处于运行状态中的Java进程,JDK自带了很多工具,允许Java开发人员监控运行进程中的各种状态,比如该进程内部创建了多少个对象实例,消耗了多少内存,等等. 本文基于JDK1.8而写成. 我下面写 ...
- Django ORM 一对一,一对多,多对多, 添加,批量插入和查询
模型类 class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_ ...
- maven项目在myeclipse中不出现Maven Dependencies 和maven标识的解决方法
这种情况通常出现在 我们新加载了一个 maven的项目,但是myeclipse没识别到. 或者说 我们把该项目修改成了maven项目--------也就是说该项目 有了pom.xml 但是还没有mav ...
- EOF与feof
在C语言中,或更精确地说成C标准函数库中表示文件结束符(end of file).在while循环中以EOF作为文件结束标志,这种以EOF作为文件结束标志的文件,必须是文本文件.在文本文件中,数据都是 ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...