标准的Trie树的题,有个小错误一直弄了很久,还是不熟练啊
/*******************************************************************************
# Author : Neo Fung
# Email : neosfung@gmail.com
# Last modified: 2011-10-04 19:17
# Filename: ZOJ1808 POJ1056 HDU1305 Immediate Decodability.cpp
# Description :
******************************************************************************/
// #include "stdafx.h"
// #define DEBUG
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <memory.h>
using namespace std;
#define KINDNUM 2
#define MAX 20000
struct TrieNode
{
bool isNum;
TrieNode *next[KINDNUM];
}node[MAX],*head;
int nodeInd;
bool insert(TrieNode *&root,char *num)
{
TrieNode *location=root;
int i=0,branch=0;
if(location==NULL)
{
location=&node[nodeInd++];
location->isNum=false;
for (int j=0;j<KINDNUM;++j)
location->next[j]=NULL;
root=location;
}
while(num[i])
{
branch=num[i]-'0';
if(location->next[branch] &&
(!num[i+1] || location->next[branch]->isNum))
return false;
else if(!(location->next[branch]))
{
location->next[branch]=&node[nodeInd++];
location->next[branch]->isNum=false;
for (int j=0;j<KINDNUM;++j)
location->next[branch]->next[j]=NULL;
}
++i;
location=location->next[branch];
if(!num[i])
location->isNum=true;
}
return true;
}
int main(void)
{
#ifdef DEBUG
freopen("data.txt","r",stdin);
#endif
char str[15];
int ncase=1;
while(gets(str) && strlen(str)!=0)
{
nodeInd=0;
head=NULL;
if(str[0]=='9') break;
printf("Set %d ",ncase++);
bool ans=insert(head,str);
while(gets(str) && str[0]!='9')
{
if(ans)
ans= ans&&insert(head,str);
}
if(ans)
printf("is immediately decodable\n");
else
printf("is not immediately decodable\n");
}
return 0;
}

本文介绍了一种使用Trie树结构来判断一组编码是否立即可解码的方法,并提供了一个C++实现示例。通过构建Trie树并检查节点状态,可以有效地验证给定的编码集合是否满足立即可解码条件。

1638

被折叠的 条评论
为什么被折叠?



