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算法. 代码如下: ...
随机推荐
- 整理oracle 树形查询
注:本文参考了<整理oracle 树形查询> sql树形递归查询是数据库查询的一种特殊情形,也是组织结构.行政区划查询的一种最常用的的情形之一.下面对该种查询进行一些总结: create ...
- 移动端点击出现阴影 css解决方案
a,img,button,input,textarea,div{-webkit-tap-highlight-color:rgba(255,255,255,0);}
- Java测试代码(很不完整,建议大家别看,过几天会再发一次难的版本)
package ATM; import java.io.BufferedReader; import java.io.InputStreamReader; class Account{ priv ...
- 《剑指offer》斐波那契数列
本题来自<剑指offer> 斐波那契数列 矩阵覆盖 题目一: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 思路: ...
- 基础运算符补充,流程控制之if判断/while循环
常量 常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量. AGE_OF_OLDBOY = 56 基础运算符补充 1.算术运算 加减乘除+ - ...
- JAVA中的Token
JAVA中的Token 基于Token的身份验证 来源:转载 最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求 ...
- GAN-生成对抗网络原理
最近一直在看GAN,我一直认为只有把博客看了一遍,然后再敲一遍.这样才会有深刻的感悟. GAN(生成式对抗网络)(GAN, Generative Adversarial Networks )是一种深度 ...
- 华硕X75VB安装ubuntu12.10网卡不可用等相关问题总结
笔记本相关信息: 电脑型号:华硕X75VB 笔记本电脑 处理器:i5-3230M 2.60GHz 双核 主板:华硕X75VB (英特尔 Ivy Bridge - HM76 Express芯片组) 内存 ...
- base | AtomicIntegerT类
1. 原子自增操作 type __sync_fetch_and_add (type *ptr, type value) 2. 原子比较和交换(设置)操作 type __sync_val_compare ...
- bash: cannot create temp file for here-document: Read-only file system
文件系统被强制只读问题,第一眼看到百度了一下,说可能磁盘坏了.卧槽我都吓懵了系统盘坏了,闹着玩呢,然后接着查资料,排查 mount 查看所有挂载,发现根目录的挂载权限是ro只读. /dev/sda2 ...