您的位置:首页 > 其它

POJ 1056 解题报告

2015-09-04 01:51 393 查看
1056和之前的3630是一样的。

thestoryofsnow1056Accepted1420K0MSC++1727B

/*
ID: thestor1
LANG: C++
TASK: poj3630
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;
const int MAXL = 10 + 1;
const int MAXN = 10000 * MAXL;

class Trie
{
public:
Trie *next[2];
bool isLeaf;
Trie()
{
init();
}

void init()
{
for (int i = 0; i < 2; ++i)
{
next[i] = NULL;
}
isLeaf = false;
}
};

Trie trie[MAXN];

bool insert(Trie *root, char number[], Trie trie[], int &trieNo)
{
bool isConsistent = false;
for (int i = 0; number[i] != '\0'; ++i)
{
int next = number[i] - '0';
// we have never been here in this test case
if (root->next[next] == NULL)
{
isConsistent = true;
root->next[next] = &trie[trieNo];
trie[trieNo].init();
// assert (root->next[next]->version < t);
trieNo++;
}

root = root->next[next];

// some words end here, so they are prefixes of this number
if (root->isLeaf)
{
return false;
}
}
root->isLeaf = true;

return isConsistent;
}

int main()
{
char number[MAXL];
Trie *root = &trie[0];
root->init();

bool isConsistent = true;
int trieNo = 1;
int t = 1;
while(scanf("%s", number) > 0)
{
if (strcmp(number, "9") == 0)
{
if (isConsistent)
{
printf("Set %d is immediately decodable\n", t);
}
else
{
printf("Set %d is not immediately decodable\n", t);
}
root->init();
isConsistent = true;
trieNo = 1;
t++;
}
else
{
if (isConsistent && !insert(root, number, trie, trieNo))
{
isConsistent = false;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: