HDU1305 Immediate Decodability (字典树
Immediate Decodability
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)
InputWrite 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).
OutputFor 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 orz一道字典树的题,以9为每组数据的间隔,给你那么多个字符串,判断是否存在一个字符串是其他某个字符串的前缀。
写法是按照给出的串构造一棵树,如果构造这条字符串的时候碰见了某个串的结尾,或者是构造完了以后接下来还有字符,就说明存在,标记一下就好了
记住每次都要清空标记。
两份ac代码,一个是别人博客学来的,一个是学长给的ppt里扒下来的字典树~第二份被没有赋{0}卡了半天orz,感谢mwjj救憨憨lj
#include<bits/stdc++.h>
using namespace std; struct node{
int a[];
int isleaf;
int haveson;
}trie[];
int len,cnt,f;
char s[]; void insert(int now,int x){
if(x==len){
if(trie[now].isleaf==||trie[now].haveson==)f = ;
trie[now].isleaf=;return ;
}
if(trie[now].isleaf==){
f = ;return ;
}
trie[now].haveson=;
int num = s[x]-'';
if(trie[now].a[num]==)trie[now].a[num]=++cnt;
insert(trie[now].a[num],x+);
}
int main(){
int t=,cas=;
while(scanf("%s",s)!=EOF){
if(s[]==''){
cas++;
if(t) printf("Set %d is not immediately decodable\n",cas);
else printf("Set %d is immediately decodable\n",cas);
memset(trie,,sizeof(trie));
t = cnt = ;
}
f = ;len=strlen(s);insert(,);
if(f==)t = ;
}
return ;
}
#include<bits/stdc++.h>
const int mod=;
const int N = 1e5+;
int a[N],n,k,tot=;
bool flag=;
using namespace std;
struct node{
bool r=;
node *next[]={};
};
node root;
bool ans = ;char s[];
void build_trie(){
int l = strlen(s);
node *p=&root;
for(int i = ;i < l;++i){
if(p->r==)flag=;
if(p->next[s[i]-'']==NULL){
p->next[s[i]-''] = new node;
}
p=p->next[s[i]-''];
}
if(p->r > )flag=;p->r=;
if(p->next[]!=NULL||p->next[]!=NULL)flag=;
} int main()
{
while(scanf("%s",s)!=EOF){
if(s[]==''){
if(ans)printf("Set %d is not immediately decodable\n",++tot);
else printf("Set %d is immediately decodable\n",++tot);
flag=ans=false;
for(int i=;i<;i++)root.next[i]=nullptr;
}
build_trie();
if(flag)ans=true;
}
return ;
}
#include<bits/stdc++.h>
using namespace std;
struct trie{
int r;
struct trie *next[];
trie(){
r = ;next[]=next[]=NULL;
}
};
int ans;
trie *root,*p,*temp;
bool insert(char str[]){
p = root;
for(int i = ;i < strlen(str);++i){
if(p->next[str[i]-'']!=NULL){
p=p->next[str[i]-''];
if(p->r==||i==strlen(str)-){
ans=;break;
}
}
else{
temp=new trie;
p->next[str[i]-'']=temp;
p=temp;
}
}
p->r = ;
}
void del(trie *root){
for(int i = ;i < ;++i){
if(root->next[i]!=NULL)
del(root->next[i]);
}
delete(root);
}
int main()
{
int t = ;ans = ;
char str[];root = new trie;
while(~scanf("%s",str)){
if(str[]==''){
if(ans) printf("Set %d is immediately decodable\n",++t);
else printf("Set %d is not immediately decodable\n",++t);
del(root);root = new trie;
ans = ;
continue;
}
if(!ans)continue;
insert(str);
}
return ;
}
HDU1305 Immediate Decodability (字典树的更多相关文章
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- (step5.1.2)hdu 1305(Immediate Decodability——字典树)
题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀.. 如果是,输出Set .. is not immediately decodable 否则输出Set . ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu1305Immediate Decodability(字典树)
这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. Problem Description An encoding of a set of symbols is said to ...
- HDU1305 Immediate Decodability(水题字典树)
巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...
- 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...
- hdu1305 字典树水题
题意: 给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路: 字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- wpscan
1版本信息检测 WPscan 使用语法 详细参数: --update #更新 -u / --url #要扫描的站点 -f / --force #不检查是否wordpress站点 -e / --enum ...
- 【Nginx】 linux环境下安装nginx步骤
开始前,请确认gcc g++开发类库是否装好,默认已经安装. centos平台编译环境使用如下指令 安装make: yum -y install gcc automake autoconf libto ...
- Thymeleaf Multiple Template Locations using Spring Boot
1. Overview In this tutorial, we'll see how we can define multiple template locations using Thymelea ...
- Button.OnClientClick
Button.OnClientClick Gets or sets the client-side script that executes when a Button control's Click ...
- linux系统创建windows启动盘
平时工作中用到linux的操作命令较多,因此为了方便,就给电脑装了双系统,一般工作的时候,都选择进入linux系统.但是今天有件工作之外的事情需要解决下:创建一个windows启动盘.如果按照往常来说 ...
- linux常用关机和重启命令
Linux有如下的关机和重启命令:shutdown, reboot, halt, poweroff,那么它们有什么区别呢? shutdown - 建议使用的命令 shutdown是最常用也是最安全的关 ...
- C++ 学习安排
第一阶段主要是理解概念及最基本的定义和声明包含以下内容:1. 头文件2. 命名空间3. 变量和基本类型4. 函数5. 类6. 标准库类型第二部分进阶入门,主要学习C++中的某些内容的特殊性及逻辑编写1 ...
- Systemd 指令
Systemd 指令 原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 一.由来 历史上,Linux 的 ...
- Extjs4 修改combox中store的数据
{ xtype: "combo", fieldLabel: '选择模板', name: "TemplateType", fieldName: "Tem ...
- PHP+实现文件的上传和下载
工程截图 配置路径 修改系统配置文件路径 填写正确的项目路径 将loclahost:811/up6/改为实际项目路径. 文件和文件夹批量上传 当网络问题导致传输错误时,只需要重传出错分片,而不是整个文 ...