poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056
思路:
字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变量用来判断当前到达的位置是否构成另一个单词的编码
代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX=;
class Trie
{
public:
bool isCode;
Trie *next[MAX];
};
bool flag; void Build_Tree(Trie *root,char *str)
{
Trie *p=root;
int i=;
while(str[i]!='\0')
{
if(p->next[str[i]-'']==NULL)
{
Trie *temp=new Trie;
for(int j=;j<MAX;j++)
temp->next[j]=NULL;
temp->isCode=false;
p->next[str[i]-'']=temp;
}
else
{
if(p->next[str[i]-'']->isCode)
{
flag=false;
return;
}
}
p=p->next[str[i]-''];
i++;
}
p->isCode=true; }
void del(Trie *root)
{
for(int i=;i<MAX;i++)
if(root->next[i]!=NULL)
del(root->next[i]);
free(root);
}
int main()
{
char str[];
int Case=;
while(scanf("%s",str)!=EOF)
{
Case++;
Trie *root=new Trie;
for(int i=;i<MAX;i++)
root->next[i]=NULL;
root->isCode=false;
flag=true;
Build_Tree(root,str);
while(scanf("%s",str))
{
if(strcmp(str,"")==)break;
if(flag) Build_Tree(root,str);
}
if(flag)cout<<"Set "<<Case<<" is immediately decodable"<<endl;
else cout<<"Set "<<Case<<" is not immediately decodable"<<endl;
del(root);
}
return ;
}
poj 1056 IMMEDIATE DECODABILITY 字典树的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- (step5.1.2)hdu 1305(Immediate Decodability——字典树)
题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀.. 如果是,输出Set .. is not immediately decodable 否则输出Set . ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
随机推荐
- flex中创建弹出窗口,并传值
在flex页面中首先创建一个弹出窗口,代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Titl ...
- require.js 源码解读——配置默认上下文
首先,我们先来简单说一下,require.js的原理: 1.载入模块 2.通过模块名解析出模块信息,以及计算出URL 3.通过创建SCRIPT的形式把模块加载到页面中. 4.判断被加载的脚本,如 ...
- JDK中日期和时间的几个常用类浅析(四)
java.time.Instant java.time.Instant类对应的是时间线上的一个时间点.该类通过保存着从格林威治的起始时间(1970年一月一日零点零分)开始计算所经过的纳妙数来表示时 ...
- div内部元素居中
要让div内部元素垂直居中,则给div加上此css样式: .div-vertical-middle{ height:200px; width:304px; line-height:50px; ...
- Linux 上做免密码登陆
基于密钥验证1.生成一对密钥ssh-keygen -t {rsa|dsa} -f /path/to/keyfile -N 'password'2.将公钥传输至服务器某用户的家目录的.ssh/autho ...
- 老李推荐:第5章5节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 获取系统服务引用
老李推荐:第5章5节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 获取系统服务引用 上一节我们描述了monkey的命令处理入口函数run是如何调用optionP ...
- 老李推荐:第2章2节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NotesList简介
老李推荐:第2章2节<MonkeyRunner源码剖析>了解你的测试对象: NotePad窗口Activity之NotesList简介 NotePad窗口Activity之NotesL ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- 刷机无法连接4g
只显示2g,gsm only 无法修改,本人刷cm13和lineageOs都遇到过这样的情况,可能与手机有关xt1570(moto x style),特在此分享,希望有用 1.首先在设置中将sim卡网 ...
- 最新windows 0day漏洞利用
利用视屏:https://v.qq.com/iframe/player.html?vid=g0393qtgvj0&tiny=0&auto=0 使用方法 环境搭建 注意,必须安装32位p ...