Immediate Decodability
题意:
在一个编码集合中,如果一个编码是另一个编码的前缀,输出Set 2 is not immediately decodable,否则输出Set 1 is immediately decodable,1,2代表第几个编码集合。
分析:
字典树模板题,求出每个编码在这个编码集合中有多少个前缀。
代码:
#include <map> #include <queue> #include <math.h> #include <string> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define ull unsigned long long #define cls(x) memset(x,0,sizeof(x)) #define clslow(x) memset(x,-1,sizeof(x)) const int maxn=1e5+100; struct TrieNode { int cnt; TrieNode* nex[2]; }; TrieNode* root; TrieNode* build() { TrieNode* node=new TrieNode; node->cnt=0; cls(node->nex); return node; } void Insert(char* s) { int len=strlen(s); TrieNode* node=root; for(int i=0;i<len;i++){ int x=s[i]-'0'; if(node->nex[x]==NULL){ node->nex[x]=build(); } node=node->nex[x]; node->cnt++; } } int Find(char* s) { int len=strlen(s); TrieNode* node=root; for(int i=0;i<len;i++){ int x=s[i]-'0'; if(node->nex[x]==NULL){ return 0; } node=node->nex[x]; } return node->cnt; } void del(TrieNode* node) { for(int i=0;i<2;i++){ if(node->nex[i]!=NULL){ del(node->nex[i]); } } free(node); } int main() { // freopen("in.txt","r",stdin); char s[20][20]; int cnt=0,kase=1; root=build(); while(scanf("%s",s[++cnt])!=EOF) { if(strcmp(s[cnt],"9")==0){ bool flag=true; for(int i=1;i<cnt;i++){ if(Find(s[i])>1) flag=false; } if(flag) printf("Set %d is immediately decodable ",kase++); else printf("Set %d is not immediately decodable ",kase++); cnt=0; del(root); root=build(); continue; } Insert(s[cnt]); } return 0; }