<题目链接>

题目大意:
给你几段只包含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树】的更多相关文章

  1. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  2. POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找

    POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...

  3. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  4. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  5. poj 3630 Phone List trie树

    Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...

  6. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  7. POJ 3630 Phone List | Trie 树

    题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...

  8. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  9. poj 1056 IMMEDIATE DECODABILITY(KMP)

    题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...

随机推荐

  1. 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载

    一:下载途径 二:本书图样 三:本书目录 第1篇 数据库运维篇第1章 数据库安装配置1.1 安装前的准备 11.2 安装数据库软件 51.2.1 方法1:OUI安装 61.2.2 方法2:静默安装 8 ...

  2. Confluence 6 使用 Apache 和 mod_proxy 的基本配置

    在这些示例中,我们使用下面的信息: http://www.example.com/confluence - 你计划使用的 URL http://example:8090/ - Confluence 当 ...

  3. ssd.pytorch

    https://towardsdatascience.com/learning-note-single-shot-multibox-detector-with-pytorch-part-1-38185 ...

  4. jsp 标签文件

    一. tag file 简介 tag file从两个方面简化了自定义标签的开发.首 先,tag file无须提前编译,直到第一次被调用才会编 译.除此之外,仅仅使用JSP语法就可以完成标签的扩 展定义 ...

  5. java和python对比----1:

    对计算来说: java 除法: 3/4 ==0; pyhton 除法: 3/4 ==0 3//4==0.75

  6. mybatis常见错误总结

    1. 现象:mybatis xml文件中查询的返回类型写成list或java.util.List时,执行sql时报 java.lang.UnsupportedOperationException错误. ...

  7. druid配置oracle遇到: 未找到要求的 FROM 关键字 errorCode 923, state 42000

    2018年05月29日 16:41:17 阅读数:518 问题背景 项目要连接oracle数据,采用的是durid连接池,但是基本配置下来,运行时发现了这个错误. 方案 可能有的一个错误就是,拼凑sq ...

  8. 步步为营-81-HttpModule(再谈Session)

    说明:session用于记录数据信息并存放在服务器内存中,但是存在一些问题.例如当使用服务器集群是会出现session丢失等情况.虽然微软提供了一些解决方案(Session进程外存储,或者存到数据库中 ...

  9. python文件操作r+,w+,a+,rb+,

    w:以写方式打开, a:以追加模式打开 (从 EOF 开始, 必要时创建新文件) r+:以读写模式打开 w+:以读写模式打开 (参见 w ) a+:以读写模式打开 (参见 a ) rb:以二进制读模式 ...

  10. https://www.cnblogs.com/zoro-robin/p/6110188.html

    https://www.cnblogs.com/zoro-robin/p/6110188.html https://blog.csdn.net/kongxx/article/details/65435 ...