Problem Links:
poj1056,Problem:
IMMEDIATE DECODABILITY
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6428 | Accepted: 3079 |
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 standard 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 9Sample Output
Set 1 is immediately decodable Set 2 is not immediately decodableSource
Pacific Northwest 1998
Solution:
Trie Tree;
Source Code:
//Mon Jul 5 15:51:51 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MMAX 100000
using namespace std;
int Count;
class TreeNode
{
public:
bool istring;
TreeNode *child[10];
};
vector<TreeNode> v(MMAX);
class TrieTree
{
private:
TreeNode root;
public:
void init();
bool insert(string word);
};
void TrieTree::init()
{
Count = 0;
root = v[Count++];
memset(root.child, NULL, sizeof(root.child));
root.istring = false;
}
bool TrieTree::insert(string word)
{
TreeNode *start = &root;
int len = word.size();
for (int i = 0; i < len; i++)
{
if (start->child[word[i] - '0'] == NULL)
{
start->child[word[i] - '0'] = &v[Count];
v[Count].istring = false;
memset(v[Count].child, NULL, sizeof(v[Count].child));
Count++;
}
else if (start->child[word[i] - '0']->istring == true || i == len - 1)
{
start->child[word[i] - '0']->istring = true;
return false;
}
start = start->child[word[i] - '0'];
}
start->istring = true;
return true;
}
int main(int argc, const char* argv[])
{
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int ncase = 0;
string str;
bool flag = true;
TrieTree x;
x.init();
while (cin >> str)
{
if (str == "9")
{
if (flag == false)
{
cout << "Set " << ++ncase << " is not immediately decodable"
<< endl;
}
else
{
cout << "Set " << ++ncase << " is immediately decodable"
<< endl;
}
flag = true;
x.init();
}
else if (x.insert(str) == false)
{
flag = false;
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
No comments :
Post a Comment