Immediate Decodability

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)

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 (字典树的更多相关文章

  1. hdu 1305 Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  2. (step5.1.2)hdu 1305(Immediate Decodability——字典树)

    题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀.. 如果是,输出Set .. is not immediately decodable 否则输出Set . ...

  3. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  4. Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. hdu1305Immediate Decodability(字典树)

    这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. Problem Description An encoding of a set of symbols is said to ...

  6. HDU1305 Immediate Decodability(水题字典树)

    巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...

  7. 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】

    HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...

  8. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

  9. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

随机推荐

  1. jquery文章链接

    好文链接 1.jQuery是js的一个库,封装了js中常用的逻辑: 2.调用jQuery: (1).本地调用,在script标签的src属性里写上jQuery文件的地址. (2).使用CDN调用jQu ...

  2. JavaScript数字计算精度丢失的问题和解决方案

    一.JS数字精度丢失的一些典型问题 1. 两个简单的浮点数相加:0.1 + 0.2 != 0.3 // true,下图是firebug的控制台截图: 看看java的计算结果:是不是让你很不能接受 再来 ...

  3. css垂直居中布局总结

    简介 总结记录一下经常需要用到垂直居中布局,欢迎补充(空手套...O(∩_∩)O) 以下栗子如果未特别标注同一使用这样的html结构 <div class="container&quo ...

  4. LC 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  5. Sql UpdateOrInsert

    SqlServer(先更新,受影响条数为0,则Insert,通过事务): begin tran update table set column=columnvalue where wherestr b ...

  6. Selenium 2自动化测试实战24(webdriver原理)

    一.webdriver原理 webdriver是按照Server-Client的经典设计模式设计的.Server端就是Remote Server,可以是任意的浏览器.当我们的脚本启动浏览器后,该浏览器 ...

  7. RxJava2实战--第二章 RxJava基础知识

    第二章 RxJava基础知识 1. Observable 1.1 RxJava的使用三步骤 创建Observable 创建Observer 使用subscribe()进行订阅 Observable.j ...

  8. 【转载】如何在 Kaggle 首战中进入前 10%

    本文转载自如何在 Kaggle 首战中进入前 10% 转载仅出于个人学习收藏,侵删 Introduction 本文采用署名 - 非商业性使用 - 禁止演绎 3.0 中国大陆许可协议进行许可.著作权由章 ...

  9. 通过正则把文本里的链接加上a标签

    把文本里的链接替换成a标签 function addLinks($text) { return preg_replace('/(http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0- ...

  10. 【D3D12学习手记】The Swap Chain and Page Flipping

    为了避免动画中的闪烁,最好将整个动画帧绘制到称为后台缓冲区的屏幕外纹理(off-screen texture)中.一旦整个场景被绘制到给定动画帧的后缓冲区,它就作为一个完整的帧呈现给屏幕;以这种方式, ...