POJ 1056 IMMEDIATE DECODABILITY 【Trie树】
<题目链接>
题目大意:
给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀。
解题分析:
Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节点的时候,判断路径上是否已经有完整的单词出现即可。
数组版:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char word[];
bool flag;
int trie[*][],cnt,ncase;
bool fp[*];
void Init(){
flag=true;
cnt=;
memset(fp,false,sizeof(fp));
memset(trie,,sizeof(trie));
}
void Insert(char *str){
int now=;
for(int i=;i<strlen(str);i++){
int to=str[i]-'';
if(!trie[now][to]){
trie[now][to]=++cnt;
}
now=trie[now][to];
if(fp[now])flag=false;
}
fp[now]=true;
}
int main(){
ncase=;
Init();
while(gets(word)){
if(word[]==''){
if(flag)printf("Set %d is immediately decodable\n",++ncase);
else printf("Set %d is not immediately decodable\n",++ncase);
Init();
continue;
}
Insert(word);
}
}
指针版:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; char word[];
bool flag;
struct Node{
bool fp;
Node *next[];
Node(){
fp=false;
for(int i=;i<;i++)
next[i]=NULL;
}
};
Node *root;
Node *now,*newnode;
void Insert(char *str){
now=root;
for(int i=;i<strlen(str);i++){
int to=str[i]-'';
if(now->next[to]==NULL){
now->next[to]=new Node;
}
now=now->next[to];
if(now->fp==true)flag=false; //如果路径上出现过完整的单词,则说明不符合
}
now->fp=true;
}
int main(){
flag=true;int ncase=;
root=new Node;
while(gets(word)){
if(word[]==''){
if(flag)printf("Set %d is immediately decodable\n",++ncase);
else printf("Set %d is not immediately decodable\n",++ncase);
flag=true;
root=new Node;
continue;
}
Insert(word);
}
return ;
}
2018-10-30
POJ 1056 IMMEDIATE DECODABILITY 【Trie树】的更多相关文章
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找
POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- poj 3630 Phone List trie树
Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- POJ 3630 Phone List | Trie 树
题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- poj 1056 IMMEDIATE DECODABILITY(KMP)
题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...
随机推荐
- Swift 学习- 03 -- 基本运算符
// 运算符是检查,改变,合并值的特殊符号或短语, // swift 支持大部分标准的 C语言的运算符, 且改进许多特性来减少常规编码错误. /* 如: 赋值符 (=) 不返回值, 以防止把想要判断相 ...
- L1和L2正则
https://blog.csdn.net/jinping_shi/article/details/52433975
- 处理:“ORA-28002: the password will expire within 7 days”的问题
一:问题描述: 二:处理步骤 [oracle@localhost 2018_07_14]$ rlwrap sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3 ...
- Confluence 6 修改日志文件的目标位置
在 log4j 中,一个输出被定义为 'appender'.希望修改 log 文件的目标,你需要停止 Confluence 然后修改设置 log4j.properties 日志配置文件的 'Logg ...
- Confluence 6 安装 SQL Server
如果你还没有在安装可以连接的 Microsoft SQL Server 数据库,请先下载后进行安装.请参考 MSDN 上 Installation for SQL Server 的指南. 有关授权模 ...
- LeetCode(103): 二叉树的锯齿形层次遍历
Medium! 题目描述: 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,nul ...
- golang 打包,交叉编译,压缩
打包,压缩 我们的常规打包方式 $ go build Mac下我们用 ls -lh查看,可以看到我们打包出来的可执行文件会比较大,一般只写几行代码就回又3M以上的文件大小了. 我们的带压缩的打包方式 ...
- springboot+mybatis+springMVC基础框架搭建
项目结构概览 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- fastJson常用方法总结
1.了解json json就是一串字符串 只不过元素会使用特定的符号标注. {} 双括号表示对象 [] 中括号表示数组 "" 双引号内是属性或值 : 冒号表示后者是前者的值(这个值 ...
- 论文阅读笔记二十:LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation(CVPR2017)
源文网址:https://arxiv.org/abs/1707.03718 tensorflow代码:https://github.com/luofan18/linknet-tensorflow 基于 ...