POJ1056

给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀

(哈夫曼编码有这个要求)

简单的过一遍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 字符串前缀查找的更多相关文章

  1. POJ 1056 IMMEDIATE DECODABILITY 【Trie树】

    <题目链接> 题目大意:给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀. 解题分析: Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节 ...

  2. poj 1056 IMMEDIATE DECODABILITY(KMP)

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

  3. poj 1056 IMMEDIATE DECODABILITY 字典树

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

  4. POJ 1056 IMMEDIATE DECODABILITY

    IMMEDIATE DECODABILITY Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9630   Accepted: ...

  5. POJ 3630 Phone List(字符串前缀重复)题解

    Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...

  6. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  7. 【数据结构与算法】Trie(前缀树)模板和例题

    Trie 树的模板 Trie 树的简介 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.他的核心思想是空间换 ...

  8. [算法2-数组与字符串的查找与匹配] (.NET源码学习)

    [算法2-数组与字符串的查找与匹配] (.NET源码学习) 关键词:1. 数组查找(算法)   2. 字符串查找(算法)   3. C#中的String(源码)   4. 特性Attribute 与内 ...

  9. Matlab 之 字符串数组查找

    Matlab的优势在于向量操作,字符串操作往往费时费力,但是如果能充分利用Matlab自带的一些函数,也可以取得不错的效果.下面就介绍一下字符串数组查找的小技巧. 字符串数组我通常会选择应用cell格 ...

随机推荐

  1. 02C语言基本语法

    C语言基本语法 注释 //行注释 /* 块注释*/ 标识符 标识符是用来标识变量.函数.任何其他用户自定义项目的名称:一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母.下划 ...

  2. 【转载】linux下的zookeeper启动

    zookeeper的安装目录:/usr/local/zookeeper-3.4.6/bin/zkServer.sh; 配置文件路径:../conf/zoo.cfg 端口 :2181: ZooKeepe ...

  3. [Algorithm] 4. Ugly Number II

    Description Ugly number is a number that only havefactors 2, 3 and 5. Design an algorithm to find th ...

  4. LES on Wind turbine

  5. Journals in Fluid Mechanics

    journal of fluid mechanics physics of fluids annual review of fluid mechanics

  6. circumferential averge streamwise velocity using Tecplot and Matlab

    Input: results from solver output: circumferential averge physical quantities( such as streamwise ve ...

  7. 【Codeforces 464A】No to Palindromes!

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为原序列没有任何长度超过2的回文串. 所以,我们在改变的时候,只要时刻保证改变位置s[i]和s[i-1]以及s[i-2]都不相同就好. 因为 ...

  8. linux常用命令大全(linux基础命令+命令备忘录+面试复习)

    linux常用命令大全(linux基础命令+命令备忘录+面试复习)-----https://www.cnblogs.com/caozy/p/9261224.html

  9. hdu 1527威佐夫博弈

    //http://www.cnblogs.com/bo-tao/archive/2012/04/16/2452633.html #include<stdio.h> #include< ...

  10. codeforces gym 100357 K (表达式 模拟)

    题目大意 将一个含有+,-,^,()的表达式按照运算顺序转换成树状的形式. 解题分析 用递归的方式来处理表达式,首先直接去掉两边的括号(如果不止一对全部去光),然后找出不在括号内且优先级最低的符号.如 ...