poj 1056 IMMEDIATE DECODABILITY(KMP)
题目链接:http://poj.org/problem?id=1056
思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法。这里为了练习KMP算法使用了KMP算法。
代码如下:
#include <iostream>
using namespace std; const int N = ;
const int Len = ;
char A[N][Len];
int Next[N][Len]; void get_nextval( char P[], int Next[] )
{
int i = , j = -;
int PLen = strlen(P); Next[] = -;
while ( i < PLen - )
{
if ( j == - || P[i] == P[j] )
{
i++;
j++;
if ( P[i] == P[j] )
Next[i] = j;
else
Next[i] = Next[j];
}
else
j = Next[j];
}
} int KMP_Matcher( char T[], char P[], int Next[] )
{
int i = , j = ;
int TLen = strlen( T );
int PLen = strlen( P ); while ( i < TLen && j < PLen )
{
if ( j == - || T[i] == P[j] )
{
i++;
j++;
}
else
j = Next[j];
} if ( j == PLen )
return i - j;
else
return -;
} int main( )
{
int Count = , flag = -, n = ; while ( scanf( "%s\n", A[Count] ) != EOF )
{
if ( A[Count][] == '' )
{
n++;
for( int i = ; i < Count; ++i )
get_nextval( A[i], Next[i] ); for ( int i = ; i < Count - ; ++i )
for ( int j = i + ; j < Count; ++j )
{
if ( flag == )
break;
flag = KMP_Matcher( A[j], A[i], Next[i] );
} if ( flag == )
printf( "Set %d is not immediately decodable\n", n );
else
printf( "Set %d is immediately decodable\n", n ); Count = ;
flag = -;
continue;
} Count++;
} return ;
}
poj 1056 IMMEDIATE DECODABILITY(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 1056 IMMEDIATE DECODABILITY 【Trie树】
<题目链接> 题目大意:给你几段只包含0,1的序列,判断这几段序列中,是否存在至少一段序列是另一段序列的前缀. 解题分析: Trie树水题,只需要在每次插入字符串,并且在Trie树上创建节 ...
- POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找
POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...
- 【POJ】1056 IMMEDIATE DECODABILITY
字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
- POJ 1961 2406 (KMP,最小循环节,循环周期)
关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406 Powe ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- CSS learnning...
"Whenever this property changes, apply that change slowly." The property transition: width ...
- typedef,static,const用法
一.typedef主要功能是定义一个已存在类型的别名,但是和宏并存 宏与typedef区别 1.宏定义只是简单的字符串替换 2.typedef定义的类型是类型的别名,typedef后面是一个整体声明, ...
- C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别
C#中网站根路径,请站点的最外一层 /表示 应用根路径 ~/表示,有时候C#程序路径并不是网站路径 物理路径 server.mappath("~/") 是指应用程序放在服务器硬盘的 ...
- Python开发环境Spyder安装方法
Spyder(Scientific PYthon Development EnviRonment)是一个强大的交互式 Python 语言开发环境,提供高级的代码编辑.交互测试.调试等特性,支持包括 W ...
- pyhton10min系列之程序员的浪漫-足迹生成器,有视频教程
记录去过的足迹 本文为原创文章 项目主页 跪求star 程序员的浪漫,我女朋友蘑菇喜欢旅游,于是我做了这个,记录2015一起去过的地方,祝她圣诞快乐 如果觉得对你有帮助,github求个star 视频 ...
- AMD模块化JS
参考http://ourjs.com/detail/52ad26ff127c76320300001f Offcial Site http://requirejs.org/ 下载http://requi ...
- QT 获取文件MD5值
/* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...
- C++模板:qsort
void qsort(int l,int r){ int i,j,t,mid; mid=b[(l+r)>>1]; i=l; j=r; do{ while (b[i]<mid) i++ ...
- uva11536 Smallest Sub-Array
Thinking about it: 我的思路跟sliding window有点类似.假设已经确定了一个区间[l, r],序列中从 l 到 r 恰好包含了[1, K]的各个元素,则从 r 开始继续迭代 ...
- hdoj 3478 Catch(二分图判定+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...