<题目链接>

题目大意:
给你几段只包含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. Jenkins三.1 配置maven

    maven配置安装下载 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-b ...

  2. Confluence 6 导入模板的步骤

    第一步:检查你 Confluence 站点中安装的模板组件 查看当前已经导入到你 Confluence 站点中可用的模板组件: 以系统管理员或者 Confluence 管理员权限登录 Confluen ...

  3. npm install Install error: Unexpected token < in JSON at position 35问题解决

    解决方案 rm package-lock.json worked.

  4. 对一个元素 同时添加单击onclick 和 双击ondblclick 触发冲突的解决

    需求说明:单击列表项内容后,吧啦吧啦,双击列表项内容后,巴拉巴拉巴拉~~~ 解决思路:卧槽 ,其实我是没思路的,当时唯一的想法就是,看个人点击鼠标的速度了,双击快一点,触发双击事件ლ(′◉❥◉`ლ), ...

  5. java爬虫笔记

    一.URl解释 1.URl统一资源定位符, Uniform Resource Location 也就是说是Internet上信息资源的字符串,所谓的网页抓取就是把URl地址中指定的网络资源从网络中读取 ...

  6. AI-CBV写法

    AI-CBV写法 CBV固定样式 #url.py from django.conf.urls import url from django.contrib import admin from app0 ...

  7. 论文阅读笔记四:CTPN: Detecting Text in Natural Image with Connectionist Text Proposal Network(ECCV2016)

    前面曾提到过CTPN,这里就学习一下,首先还是老套路,从论文学起吧.这里给出英文原文论文网址供大家阅读:https://arxiv.org/abs/1609.03605. CTPN,以前一直认为缩写一 ...

  8. Asp.Net MVC Ajax轮训解决Session失效时间

    这种方法不是太好,对服务器得压力大,由于系统是内部人员使用,业务有比较复杂,所以有些值得需要Session去保存,但是,Session有失效时间. 代码如下: $(function () { func ...

  9. 【APUE | 03】文件I/O

    博客链接: inux中的文件描述符与打开文件之间的关系 #include <stdio.h> #include <unistd.h> #include <sys/stat ...

  10. MyBatis - 2.全局文件配置

    1.properties 属性 <!--properties 引入外部配置文件 properties 的内容 resource: 引入类路径资源 url: 引入网络资源 --> <p ...