题目:
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
题意:
给你一组仅有01构成的字符串集合,每组测试数据用"9"隔开。如果该组字符串中存在某些字符串是另一些字符串的前缀,则输出"Set 第几组 is not immediately decodable",否则为"Set 第几组 is immediately decodable"。
这道题是在01字符串集合中找出是否存在某些串是另一些串的前缀,可以用字典树解决。
字典树又称单词查找树,典型应用于统计,排序和保存大量的字符串等。它是利用字符串的公共前缀来节约存储空间。
字典树就类似于我们平时查英语单词,我们都是先查找第一个字母所在的范围,然后在这基础之上查找第二个字母所在的范围,依此类推。同样,字典树也与这个类似。
参考代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int N = 26;//编码: 0 or 1;//实际上N可以取2;
const int MAX = 8+2;//each set has at least two codes and no more than eight;
struct Trie {
Trie *next[N];//next是表示每层有多少种类的数;
int v;//表示一个字典树到此有多少相同前缀的数目;
};
Trie *root;
vector<string> str;
void createTrie(string str) {//创建字典树, 将该字符串里的编码加入字典树;
int len = str.length();//该字符串的长度;
Trie *p = root, *q;//p从根结点出发;
for (int i = 0;i < len;++i) {
int id = str[i] - '0';//将该字符串转换为下标;
if (p -> next[id] == NULL) {//表明该字符没有连接到字典树;//第一个字符;
q = new Trie;//新增结点;
q -> v = 1;//初始v == 1;//到此有一个相同前缀;
for (int j = 0;j < N;++j) {
q -> next[j] = NULL;//该结点的下一个结点暂时什么也没有;
}
p -> next[id] = q;//根结点连接q结点;//链路表示为下标对应的编码;
p = p -> next[id];//p值更新;
}
else {
p -> next[id] -> v++;//相同前缀的数目增加;
p = p -> next[id];//p值更新;
}
}
p -> v = -1;//若为结尾, 则将v改成-1表示;
}
int findTrie(string str) {//查找;
int len = str.length();
Trie *p = root;
for (int i = 0;i < len;++i) {
int id = str[i] - '0';
p = p -> next[id];
if (p == NULL) {//若为空集, 表示不存在以此为前缀的串;
return 0;
}
if (p -> v == -1) {//字符集中已有的串是此串的前缀;
return -1;
}
}
return -1;//此串是字符集中某串的前缀;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
int cnt = 0;
while (cin >> s) {
if (s != "9") {
str.push_back(s);
}
else {
bool flag = true;
root = new Trie;//创建一个根结点;
root -> next[0] = NULL;//初始情况, 这棵树还没有字符串;
root -> next[1] = NULL;
root -> v = 1;
map<string, int> m;
m.clear();
for (auto e : str) {
int judge = findTrie(e);//查找前缀;
if (judge == -1) {
++m[e];
}
createTrie(e);
}
for (map<string, int>::iterator it = m.begin();it != m.end();++it) {
if (it -> second > 0) {//不包括自己;
flag = false;
break;
}
}
if (flag) cout << "Set " << ++cnt << " is immediately decodable" << endl;
else cout << "Set " << ++cnt << " is not immediately decodable" << endl;
delete root;
str.clear();
}
}
return 0;
}