UVA - 644 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
判断是否有字符串是其他字符串的前缀。
简单字典树。(人生第一个字典树的题目)。
//Asimple #include <bits/stdc++.h> //#define INF 0x3fffffff #define mod 10007 #define CLS(a, v) memset(a, v, sizeof(a)) #define debug(a) cout << #a << " = " << a <<endl using namespace std; typedef long long ll; const int maxn = 20000+5; const double PI=acos(-1.0); const int INF = ( 1 << 31 ) ; ll n, m, res, ans, len, T, k, num, sum, x, y; struct node{ int f; node *nx[2]; }; node *newnode(){ node *p = new node; p->f = 0; p->nx[0] = NULL; p->nx[1] = NULL; return p; } int get_tree(node* root, char* s){ int i, l = strlen(s); node *p = root; for(i=0; i<l; i++) { int k = s[i]-'0'; if( p->f ) return 0; if( p->nx[k]==NULL ) p->nx[k] = newnode(); p = p->nx[k]; } if( p->nx[0]==NULL && p->nx[1]==NULL && p->f==0) { p->f = 1; return 1; } else return 0; } void free_node(node* p){ if( p->nx[0] ) free_node(p->nx[0]); if( p->nx[1] ) free_node(p->nx[1]); free(p); }
void input() { ios_base::sync_with_stdio(false); char s[20][20]; int n = 0, num = 0; node* head; while( scanf("%s", s[n++])!=EOF ) { if( s[n-1][0]!='9'); else { n --; num++; head = newnode(); int i; for(i=0; i<n; i++) { int k = get_tree(head, s[i]); if( k == 0 ) break; } if(i==n) printf("Set %d is immediately decodable\n", num); else printf("Set %d is not immediately decodable\n", num); free_node(head); n = 0; } } } int main(){ input(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2016-05-22 ACM题目————The Blocks Problem
2016-05-22 ACM学习之路————一个大整数与一个小整数不得不说得的秘密
2016-05-22 ACM题目————Anagram
2016-05-22 ACM题目————马拦过河卒