poj 1056 Trie树判断哈夫曼编码是否合法
来源:互联网 发布:淘宝白酒真假 编辑:程序博客网 时间:2024/05/20 13:39
理解了Trie树然后就能1A 其实估计这个题随便做做就能A掉,可能不需要高级数据。
先贴吉林大学的代码模板
/*==================================================*\| Trie树(k叉)| INIT: init();| 注: tree[i][tk]>0时表示单词存在, 当然也可赋予它更多含义;\*==================================================*/const int tk = 26, tb = 'a'; // tk叉; 起始字母为tb;int top, tree[N][tk + 1]; // N: 最大结点个数 top: 第一层有多少节点void init(){ top = 1; memset(tree[0], 0, sizeof(tree[0]));}int sear(char*s){ // 失败返回0 for(int rt = 0; rt = tree[rt][*s - tb]; ) if(*(++s) == 0) return tree[rt][tk]; //插入单词结束的时候 有这一句 tree[rt][tk] = Rank; return 0;}void Insert(char*s, int Rank = 1){ int rt, nxt; for(rt = 0; *s; rt = nxt, ++s) { nxt = tree[rt][*s - tb]; if(0 == nxt) {//没被使用时插入 tree[rt][*s - tb] = nxt = top; memset(tree[top], 0, sizeof(tree[top])); top++; } } tree[rt][tk] = Rank;//1表示存在0表示不存在,也可以赋予其其他含义}void delt(char*s){ // 只做标记, 假定s一定存在 int rt = 0; for( ; *s; ++s) rt = tree[rt][*s - tb];//tree的值本身指向下一个节点 tree[rt][tk]=0;}int prefix(char*s){ // 最长前缀 int rt = 0, lv; for(lv = 0; *s; ++s, ++lv) { rt = tree[rt][*s - tb]; if(rt == 0) break; } return lv;}
我把模板改掉然后A了poj1056
#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int N = 2000+10;char str[N];const int tk = 26, tb = '0'-5; // tk叉; 起始字母为tb;int top, tree[N][tk + 1]; // N: 最大结点个数 top: 第一层有多少节点void init(){ top = 1; memset(tree[0], 0, sizeof(tree[0]));}int Insert(char*s, int Rank = -2){//-2表示一个单词的结尾 int rt, nxt; for(rt = 0; *s; rt = nxt, ++s) { nxt = tree[rt][*s - tb]; if(tree[rt][tk]<0)return -2; //这是我改的地方,原来的模板,在 tree[rt][tk] = Rank;表示结束(相当于空字符) //那么每走到一步查看是不是以前的单词到这里是结尾,如果是的话,说明这之前的都匹配了,当然就不合法 if(0 == nxt) {//没被使用时插入 tree[rt][*s - tb] = nxt = top; memset(tree[top], 0, sizeof(tree[top])); top++; } } tree[rt][tk] = Rank;//1表示存在0表示不存在,也可以赋予其其他含义 return 0;}int main(){ int ans=0,cnt=1,flag=0; init(); while(~scanf("%s",str) && strcmp(str,"9")) { ans=flag=0; ans=Insert(str); if(ans<0)flag=1; while(scanf("%s",str) && strcmp(str,"9")) { ans=Insert(str); if(ans<0)flag=1; } if(flag){printf("Set %d is not immediately decodable\n",cnt++);} else { printf("Set %d is immediately decodable\n",cnt++); } init(); }}
其实我的代码这组数据就过不了
10
010
01
0000
9
题中好像没说已经按长度排过序了,但是测试数据肯定是排序过的
应该加上判断,如果*(s+1)==0 && nxt!=0,那么说明已经有比现在正在插入的长的字符串,当前正在插的字符串是已经插的前缀。。。
1 0
- poj 1056 Trie树判断哈夫曼编码是否合法
- poj 3332-判断字符串是否合法
- Trie树 POJ 1056
- poj 2001/1056 Trie树(求单词表的最短前缀/判断立即码)
- POJ 1056 IMMEDIATEDECODABILITY(字典树Trie)
- POJ -- 1056 IMMEDIATE DECODABILITY (Trie 树)
- POJ 1056 解题报告 Trie 树
- POJ-1056(Trie)
- POJ 3630 Trie树
- POJ 2001 Trie树
- poj 2001 trie 树
- POJ 3630 Trie树
- poj 3630 trie树
- POJ 1204 Trie树
- Trie树+poj
- poj 1997 trie树
- POJ 2945 trie树
- POJ 3630 trie树
- Zip包格式的MySQL的安装,启动,关闭和卸载和1067的错误,登录,修改初始密码
- BlazeDS 整合 Flex HelloWorld 示例
- Hadoop技术内幕-远程过程调用基础知识
- VS2012配置64bit的GSL
- vs中bitmap等图标文件你在哪?
- poj 1056 Trie树判断哈夫曼编码是否合法
- FreeMarker整合Spring 3
- 2014年腾讯游戏实习生面试体验
- 汇编入门学习笔记 (三) —— 第一个程序
- pthread线程编程常用API
- Unity3d Resources TextAsset 文本
- TABLE ACCESS BY INDEX ROWID前面的星号
- 用位操作实现的数据交换
- POJ 3905 Perfect Election(简单2-SAT)