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事件之解绑事件

    语法: $(selector).unbind([eventType][,handler(eventObject)]); 返回值:jQuery 参数解释: eventTypey:类型:String以后包 ...

  2. 第六天-css基础(css定位)

    css定位   方位名称:  left  right  top  bottom   position:absolute 绝对定位(脱离标准流 div{ width:100px; height:100p ...

  3. 「CF803C」 Maximal GCD

    题目链接 戳我 \(Solution\) 令\(gcd\)为\(x\),那么我们将整个序列\(/x\),则序列的和就变成了\(\frac{n}{x}\),所以\(x\)必定为\(n\)的约数所以现在就 ...

  4. 对iOS锁的一些研究

    #import <objc/runtime.h> #import <objc/message.h> #import <libkern/OSAtomic.h> #im ...

  5. 关于Sass和Less牵扯的问题

    关于Sass和Less牵扯的问题 关于Sass和Less Sass和Less都算是一种编程语言(后面会详谈此处牵扯出来的编程语言),都是CSS预处理器,都具有相同的功能,可以帮助我们快速编译CSS代码 ...

  6. [学习笔记] Tangent Distance

    Tangent Distance 简介 切空间距离可以用在KNN方法中度量距离,其解决的是图像经过有限变换之后还能否被分类正确,例如.对一张数字为5的手写数字图片,将其膨胀后得到图像p1,此时KNN还 ...

  7. JAVA记事本的图形用户界面应用程序含加密

    JAVA记事本的图形用户界面应用程序 加密 题目简介: 整体分析: 实验代码: import java.awt.EventQueue; import java.awt.event.ActionEven ...

  8. (4)rapidxml的详解及使用

        RapidXml是指 XML DOM解析工具包,是一个快速的读写xml文件的库文件(hpp).     (1)创建XML文件 #include <iostream> #includ ...

  9. 物料主数据批导bapi

    创建物料主数据,根据模板不同批导原材料,半成品,成品.可根据实际需求对字段进行增删. report zmmr_bapi_mm01 no standard page HEADING. type-POOL ...

  10. [笔记] Ubuntu 18.04安装Docker CE及NVIDIA Container Toolkit流程

    之前写的[笔记] Ubuntu 18.04安装Docker CE及nvidia-docker2流程已经out了,以这篇为准. Docker的好处之一,就是在Container里面可以随意瞎搞,不用担心 ...