POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找
给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀
(哈夫曼编码有这个要求)
简单的过一遍Trie就可以了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxl=1e+4,maxc=2,BASE='0'; struct Trie
{
int tot,root,child[maxl][maxc];
bool flag[maxl];
Trie()
{
memset(child[1],0,sizeof(child[1]));
flag[1]=false;
root=tot=1;
}
void insert(const char * str)
{
int *cur=&root;
for(const char *p=str;*p;++p)
{
cur=&child[*cur][*p-BASE];
if(*cur==0)
{
*cur=++tot;
memset(child[tot],0,sizeof(child[tot]));
flag[tot]=false;
} }
flag[*cur]=true;
}
bool query(const char *str)
{
int *cur=&root;
const char *p;
for(p=str;*p&&*cur;++p)
{
cur=&child[*cur][*p-BASE];
if(flag[*cur]==true)return false;
}
if((*cur==0))return true;
if(!(*p))return false;
else return true;
}
}; int main()
{
bool flagg;
int tott=0;
do
{ tott++;
flagg=false;
Trie tr;
char c[1000];
bool ans=true;
int len=0;
while(true)
{
char nowc=getchar();
if(nowc==EOF)return 0;
if(nowc=='9'){flagg=true;getchar();break;}
if(nowc!='\n')c[len++]=nowc;
else
{
c[len++]='\0';
if(!tr.query(c)){ans=false;}
tr.insert(c);
len=0;continue;
}
}
if(ans){printf("Set %d is immediately decodable\n",tott);}
else {printf("Set %d is not immediately decodable\n",tott);}
} while(flagg);
return 0;
}
POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找的更多相关文章
- POJ 1056 IMMEDIATE DECODABILITY 【Trie树】
<题目链接> 题目大意:给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀. 解题分析: Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节 ...
- poj 1056 IMMEDIATE DECODABILITY(KMP)
题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 1056 IMMEDIATE DECODABILITY
IMMEDIATE DECODABILITY Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9630 Accepted: ...
- POJ 3630 Phone List(字符串前缀重复)题解
Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 【数据结构与算法】Trie(前缀树)模板和例题
Trie 树的模板 Trie 树的简介 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.他的核心思想是空间换 ...
- [算法2-数组与字符串的查找与匹配] (.NET源码学习)
[算法2-数组与字符串的查找与匹配] (.NET源码学习) 关键词:1. 数组查找(算法) 2. 字符串查找(算法) 3. C#中的String(源码) 4. 特性Attribute 与内 ...
- Matlab 之 字符串数组查找
Matlab的优势在于向量操作,字符串操作往往费时费力,但是如果能充分利用Matlab自带的一些函数,也可以取得不错的效果.下面就介绍一下字符串数组查找的小技巧. 字符串数组我通常会选择应用cell格 ...
随机推荐
- 14Oracle Database 高级事务,游标
Oracle Database 高级事务,游标 隔离级别 脏读 不可重复读 虚读 读未提交 Read uncommitted 可以 可以 可以 读已提交 Read committed 不可以 可以 可 ...
- canvas练手项目(三)——Canvas中的Text文本
Canvas中的Text文本也是一个知识点~,我们需要掌握一下几个基本的Text操作方法 首先是重要参数textAlign和textBaseline: textAlign left center ri ...
- JavaScript ES6 数组新方法 学习随笔
JavaScript ES6 数组新方法 学习随笔 新建数组 var arr = [1, 2, 2, 3, 4] includes 方法 includes 查找数组有无该参数 有返回true var ...
- [Algorithm] 6. Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...
- linux网络不通,如何解决
(Network is unreachable)表示网络不可达,先ping一个外网网段检测是否通畅,(如ping不通)检查防火墙是否阻挡,网关是否正确,再检查网卡配置文件(注:网卡配置顺序错误,也可导 ...
- 你相信吗??Python把数字也当做对象!@@@对象,名称绑定,引用计数
本文学习自:http://blog.csdn.net/yockie/article/details/8474408 1.对象 Python中, 万物皆对象,包括12345等int常量.不信吗??用di ...
- xfce 设在分辨率1920 1080
#自定义cvt 1920 1080 #查看系统显示器名称xrandr #设置分辨率xrandr --newmode "1920x1080_60.00" 173.00 1920 20 ...
- codeforces 373 A - Efim and Strange Grade(算数模拟)
codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...
- 设置mysql5.7远程连接-----------https://blog.csdn.net/qiyueqinglian/article/details/52778230
https://blog.csdn.net/qiyueqinglian/article/details/52778230 设置mysql5.7远程连接
- tmux使用入门
tmux是Linux中窗口管理程序,适用于终端复用,尤其适合远程连接.最近,我正苦闷与ssh自动超时退出和broken pipe,决定投入tmux怀抱. 使用tmux最直接的好处,便是可以在一个远程连 ...