#10052. 「一本通 2.3 练习 1」Immediate Decodability
大意:给一些数字串,问是否有数字串是另一数字串的前缀。 字典树板子
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
int tre[maxn][11],sz;
struct node{
char A[11];
int len;
}mp[maxn];
int n,m;
bool cmp(node a,node b){
return a.len>b.len;
}
bool solve(int t){
int len = mp[t].len,now=0;
for(int i=0;i<len;++i){
int tem = mp[t].A[i] - '0';
if(i==len-1&&tre[now][tem]) return 0;
if(!tre[now][tem]) tre[now][tem] = ++sz;
now = tre[now][tem];
}
return 1;
}
int main(){
int _=1;
while(~scanf("%s",mp[0].A)){
mp[0].len = strlen(mp[0].A);
sz = 0;
memset(tre,0,sizeof(tre));
for(n = 1;;++n){
scanf("%s",mp[n].A);
int t = strlen(mp[n].A);
if(t==1&&mp[n].A[0]=='9') break;
mp[n].len = t;
}
sort(mp,mp+n,cmp);
int flag = 0;
for(int i=0;i<n;++i){
if(!solve(i)){
flag = 1;
break;
}
}
if(flag) printf("Set %d is not immediately decodable\n",_++);
else printf("Set %d is immediately decodable\n",_++);
}
return 0;
}