题目链接: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)的更多相关文章

  1. poj 1056 IMMEDIATE DECODABILITY 字典树

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

  2. POJ 1056 IMMEDIATE DECODABILITY

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

  3. POJ 1056 IMMEDIATE DECODABILITY 【Trie树】

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

  4. POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找

    POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> ...

  5. 【POJ】1056 IMMEDIATE DECODABILITY

    字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...

  6. POJ 3450 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...

  7. POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406  Powe ...

  8. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  9. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. 关于ue上传图片到七牛云设置key

    多图上传设置key: dialogs文件下面,image文件下面的image.html,链接webuploader.js,不链接webuploader.min.js webuploader.js里面 ...

  2. HDU - 1116 Play on Words(欧拉图)

    有向图是否具有欧拉通路或回路的判定: 欧拉通路:图连通:除2个端点外其余节点入度=出度:1个端点入度比出度大1:一个端点入度比出度小1 或 所有节点入度等于出度 欧拉回路:图连通:所有节点入度等于出度 ...

  3. C语言:类似linux内核的分等级DEBUG宏(打印宏)

    总结几种log打印printf函数的宏定义 http://blog.chinaunix.net/uid-20564848-id-73402.html #include <stdio.h> ...

  4. ThinkPHP第二十天(getField用法、常用管理员表结构、树形结构前小图标CSS)

    1.getField($fields,$sepa=null) A:当$fields为1个字段,$sepa=null的时候,返回一个符合条件的记录的字段. B:如果要取得所有符合条件记录字段,需要$se ...

  5. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

  6. 解决IDAPython: importing "site" failed.的问题

    当我打开IDA6.8时候,里面报Warning, IDAPython: importing "site" failed. WTF!? 我点了OK后,进去发现IDA底部的python ...

  7. C# 读书笔记之继承与多态

    1.1继承与多态的基本概念 1.1.1 继承和多态 继承是面向对象程序设计的主要特征之一,允许重用现有类(基类,亦称超类.父类)去创建新类(子类,亦称派生类)的过程.子类将获取基类的所有非私有数据和行 ...

  8. 安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 我们安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机,我们可以通过VMware vSphere Client直接 ...

  9. 利用bind搭建dns

    下载bind,我下载的是bind-9.3.1rc1.tar.gz 我下载的文件放在/root目录下 进入目录解压缩 [root@linux root]#tar xfz bind-9.3.1rc1.ta ...

  10. BZOJ 1022

    program bzoj1022; var t,n,i,ans,k,j,k1,k2:longint; bo:boolean; begin read(t); to t do begin read(n); ...