HDU1305 Immediate Decodability (字典树

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 ;
}

上一篇:React Native踩坑Tip


下一篇:转载:python + requests实现的接口自动化框架详细教程