Immediate Decodability(C++ Stl -----substr判断前缀)
2015-08-30 09:15
489 查看
Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2329 Accepted Submission(s): 1205
Problem Description
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 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).
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.
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
Source
Pacific Northwest 1998
#include<cstdio> #include<iostream> #include<cstring> #include<vector> #include<algorithm> using namespace std; vector<string>vec; int main() { string s; int i,success=1,times=1; while(cin>>s) { if(s=="9") { sort(vec.begin(),vec.end()); for(i=1;i<vec.size();i++) { if(vec[i].substr(0,vec[i-1].size())==vec[i-1]) { success=0; break; } } if(success==1) { printf("Set %d is immediately decodable\n",times++); } else { printf("Set %d is not immediately decodable\n",times++); } vec.clear(); success=1; } else { vec.push_back(s); } } }
相关文章推荐
- C语言字符串处理函数库
- Vc++ 控件用法总结之List Control
- C++ Primer : 第十二章 : 文本查询程序
- malloc、calloc、realloc的区别
- C++11 并发
- C语言中sizeof与strlen区别
- C++ string类
- 通过实例浅析Python对比C语言的编程思想差异
- LeetCode-Binary Tree Paths -解题报告
- C++写dll文件用C#调用
- C语言中运算符及其优先级
- c++多态原理
- 设计模式在游戏中的应用--观察者模式(十)
- Effective C++——条款10条,条款11和条款12(第2章)
- Fedora下Eclipse建立c++工程的"Unresolved inclusion: <iostream>"问题
- 关于c++字符串的while(*temp++)
- c++字符串学习
- Apriori算法及C++实现
- ptmalloc,tcmalloc和jemalloc内存分配策略研究
- 编程经验C++