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算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
随机推荐
- 【CUDA 基础】3.2 理解线程束执行的本质(Part I)
title: [CUDA 基础]3.2 理解线程束执行的本质(Part I) categories: CUDA Freshman tags: 线程束分化 CUDA分支 toc: true date: ...
- Python3学习笔记(十七):requests模块
官方中文文档:http://docs.python-requests.org/zh_CN/latest/
- YY的GCD【luoguP2257】
题目大意 有至多\(10000\)组询问,问\(1 < i \leqslant N \leqslant 10000000, 1 < j \leqslant M \leqslant 1000 ...
- Java 学习(六)
Java 学习(六) 标签(空格分隔): Java 枚举 JDK1.5引入了新的类型--枚举.在 Java 中它虽然算个"小"功能,却给我的开发带来了"大"方便 ...
- Javascript引擎的单线程机制和setTimeout执行原理阐述
工作中使用setTimeout解决了一个问题,于是对setTimeout的相关资料整理了下,以及对js引擎执行的原理一并整理了下,希望能给码农们一些帮助.若发现有错的地方大家及时指出,共同学习进步. ...
- Travis-CI自动化测试并部署至自己的CentOS服务器
一直都想自己部署一下自动化测试部署,在了解了Travis-CI之后终于准备在这次和小伙伴一起做的一个博客类网站实验下了. 因为这是一个前后端分离的项目,所以我这里只管前端工程的自动化部署,前端主要用V ...
- Maven-指定要打包的文件
在项目 pom.xml 中指定 <build> <resources> <resource> <!--控制资源目录下要打包进去的文件,这里为全部打包--> ...
- 十大经典排序算法最强总结(含JAVA代码实现)(转)
十大经典排序算法最强总结(含JAVA代码实现) 最近几天在研究排序算法,看了很多博客,发现网上有的文章中对排序算法解释的并不是很透彻,而且有很多代码都是错误的,例如有的文章中在“桶排序”算法中对每 ...
- Vue踩坑系列
前言 前端开发对于vue的使用已经越来越多,它的优点就不做介绍了, 本篇是我对vue使用过程中遇到的问题中做的一些总结,帮助大家踩坑.如果喜欢的话可以点波赞,或者关注一下,希望本文可以帮到大家!!! ...
- Linux命令之文件和目录操作命令(一)—— ls
本文介绍了ls命令的最常用法. 1.用法: ls [选项][目录名或文件名] 2.选项: -l 使用长格式显示目录或文件的详细信息 -a 显示隐藏文件 -h 人性化显示文件或目录的大小 -d 显示目录 ...