题意很好理解,这里只介绍思路:
以前做字典树,为了方便都是静态分配内存,所以代码写出来很容易出bug,学了强连通之后,发现动态分配内存狠给力,所以就尝试用动态分配内存做了两道题目:
题很简单,自己只是想学习一下动态分配内存
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
using namespace std;
int flag=0;
struct tree
{
bool vis;
int count;
struct tree *next[10];
tree()
{
vis=false;
count=0;
for(int i=0; i<10; i++)
{
next[i]=NULL;
}
}
}*root;
struct node
{
int len;
char ans[20];
}Node[11000];
void Build_tree(char *word)
{
struct tree *st,*tem;
st=root;
while(*word)
{
if(st->next[*word-'0']==NULL)
{
tem=new tree();
st->next[*word-'0']=tem;
}
if(st->vis)
{
flag=1;
return ;
}
st=st->next[*word-'0'];
word++;
}
st->count++;
st->vis=true;
}
void freedom(struct tree *r)
{
for(int i=0; i<10; i++)
if(r->next[i]!=0)
freedom(r->next[i]);
free(r);
}
int cmp1(const void* a,const void *b)
{
node *aa=(node*)a;
node *bb=(node*)b;
return aa->len - bb->len;
}
int cmp(node a,node b)
{
return a.len<b.len;
}
int main()
{
int step=0;
int T,t;
root=new tree();
int big=-1;
memset(Node,0,sizeof(Node));
scanf("%d",&T);
while(T--)
{
scanf("%d",&t);
while(t--)
{
scanf("%s",&Node[++big].ans);
Node[big].len=strlen(Node[big].ans);
}
qsort(Node,big+1,sizeof(Node[0]),cmp1);
for(int i=0; i<=big; i++)
{
if(flag==1)
break;
Build_tree(Node[i].ans);
}
if(flag==1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
flag=0;
freedom(root);
root=new tree();
big=-1;
}
return 0;
}
附一道类似的题目:该题数据比较水,for()循环可以解决=。。=
Description

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)
Input
Write a program that accepts as input a series of groups of records from a data file. 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).
Output
For 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.
The Sample Input describes the examples above.
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
using namespace std;
int flag=0;
struct tree
{
bool vis;
struct tree *next[3];
tree()
{
vis=false;
for(int i=0; i<3; i++)
{
next[i]=NULL;
}
}
}*root;
struct node
{
int len;
char ans[20];
} Node[11000];
void Build_tree(char *word)
{
struct tree *st,*tem;
st=root;
while(*word)
{
if(st->next[*word-'0']==NULL)
{
tem=new tree();
st->next[*word-'0']=tem;
}
if(st->vis)
{
flag=1;
return ;
}
st=st->next[*word-'0'];
word++;
}
st->vis=true;
}
void freedom(struct tree *r)
{
for(int i=0; i<3; i++)
if(r->next[i]!=0)
freedom(r->next[i]);
free(r);
}
int cmp1(const void* a,const void *b)
{
node *aa=(node*)a;
node *bb=(node*)b;
return aa->len - bb->len;
}
int cmp(node a,node b)
{
return a.len<b.len;
}
int main()
{
int step=0;
int T,t;
root=new tree();
int big=-1;
memset(Node,0,sizeof(Node));
while(scanf("%s",&Node[++big].ans)!=EOF)
{
if(Node[big].ans[0]!='9')
Node[big].len=strlen(Node[big].ans);
if(Node[big].ans[0]=='9')
{
sort(Node,Node+big,cmp);
for(int i=0; i<big; i++)
{
if(flag==1)
break;
Build_tree(Node[i].ans);
}
if(flag==1)
{
printf("Set %d is not immediately decodable\n",++step);
}
else
{
printf("Set %d is immediately decodable\n",++step);
}
flag=0;
freedom(root);
root=new tree();
big=-1;
memset(Node,0,sizeof(Node));
}
}
return 0;
}