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 ...
随机推荐
- Mysql++详解
MySQL++数据库操作(1).mysqlpp::Connection con(false); //定义了mysql连接对象: (2).con.set_option(new mysqlpp::SetC ...
- iOS面试题04-runtime
runtime/KVO等面试题 1.KVO内部实现原则 回答:1>KVO是基于runtime机制实现的 2>当某个类的对象第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个 ...
- zoj 2376 Ants
#include<stdio.h> #include<stdlib.h> ]; int main(void) { int t,n,m,i,len,max,min,mx,mi; ...
- ADO.NET知识的运用一(Day 26)
哈哈,又到了总结的时间了,来回顾一下今天主要学了关于ADO.NET的哪些知识吧.(这次学的ADO访问数据库主要以访问SQL数据库为主) 理论: 首先我们要知道为什么要学习ADO.NET? 因为我们之 ...
- BZOJ 1296: [SCOI2009]粉刷匠( dp )
dp[ i ][ j ] = max( dp[ i - 1 ][ k ] + w[ i ][ j - k ] ) ( 0 <= k <= j ) 表示前 i 行用了 j 次粉刷的机会能正 ...
- jquery选择器:nth-child()与空格:eq() 的区别;
在一个7x7的表格当中 nth-child(1); td.parents("table").find("tr :nth-child(1)").css(" ...
- Java web 开发环境配置。
一.配置 win8 64位 环境java 开发环境 1. 下载JDK,地址 http://www.oracle.com/technetwork/java/javase/downloads/index ...
- angularjs学习总结(快速预览版)
对html标签的增强 -> 指令 指令的本质是什么 声明的方式调用相应的脚本,实现一些操作,声明的所在的dom就是脚本的执行上下文? 自定义标签 -- 标签指令自定义属性 -- 属性指令特定格式 ...
- HighCharts学习
http://www.stepday.com/topic/?369 http://www.helloweba.com/view-blog-156.html
- linux技术框架
编程语言 一般使用c或者c++ linux使用 鸟哥私房菜 工具使用 代码编辑source insight,代码编译gcc,代码调试gdb,代码编译组织makefile,命令执行shell,文本编辑n ...