时间限制: 1 Sec 内存限制: 64 MB
题目描述
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)
输入
Write a program that accepts as input a series of groups of records from standard 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).
输出
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.
样例输入
0110001000009011001000009
样例输出
Set 1 is immediately decodableSet 2 is not immediately decodable
提示
来源
本题是一个字典树的经典例题,在插入的过程中对字符串最后一个位置在树上标记,防止前缀子串先出现的情况(也可以用vector先对存入的子串进行存储,然后从长到短依次存储)。
#include <bits/stdc++.h>
#define cl(x) memset(x,0,sizeof(x))
#define _cl(a,b) memset(a,0,(b+1)<<2)
#define At(x,y) memset(x,y,sizeof x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define vct vector
#define pq priority_queue
#define all(x) x.begin(),x.end()
#define int long long
#define fi first
#define se second
//#pragma GCC optimize(2)
using namespace std;
inline int min(int a,int b) {return a<b?a:b;}
inline int max(int a,int b) {return a>b?a:b;}
//map<string,map<string,int> > mp;
//pq <ll,vct<ll>,less<ll> > now,last;
//vct<vct<int> > vt(n+1,vct<int>(m+1,0))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const int MOD=998244353;
const int N=1e2+10;
int n,m;
int son[N][10],idx,vi[N];
int f;
string s;
void insert(string s)
{
int p=0,len=s.size();
for(int i=0;i<len;i++)
{
int u=s[i]-'0';
if(!son[p][u]) son[p][u]=++idx;
else if(i==len-1) f=1;
if(vi[p]) f=1;
p=son[p][u];
}
vi[p]=1;
}
void solve()
{
int c=0;
while(cin>>s){
if(s!="9") insert(s);
else{
c++;
if(f) printf("Set %d is not immediately decodable\n",c);
else printf("Set %d is immediately decodable\n",c);
f=0,idx=0;
cl(son);
cl(vi);
}
}
return ;
}
signed main()
{
// srand(time(NULL));
// freopen("2022_XinShengSai_1.in", "r", stdin);
// freopen("2022_XinShengSai_1.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int t;
// cin>>t;
t=1;
while(t--)
solve();
return 0;
}
/*
*/