题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763

题意就是求s串中满足EAEBE格式的E的最大长度;我们可以枚举前缀和后缀的所有匹配(k)看是否在s[k,len-k]中;

如果不在它中间那么就让k=Next[k],刚开始想的是k--;但是这样循环次数有点多(本题数据太水,k--也能过);

但是s[0,k-1]和s[len-k-1,len-1]不一定一样啊,如果是Next[k]的话那么前缀Next,即前缀的前缀和后缀肯定相等并且等于后缀的后缀(有点模

糊好好想想就明白了。。。)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int N = 1e6+; int Next[N];
char s[N], ss[N], sm[N]; void GetNext(char s[], int n)
{
int i=, j=-;
Next[] = -;
while(i<n)
{
if(j==- || s[i]==s[j])
Next[++i] = ++j;
else
j = Next[j];
}
}
bool kmp(char a[], int n, char b[], int m)
{
int i=, j=;
while(i < m)
{
if(j==- || a[j] == b[i])
i++,j++;
else
j = Next[j];
if(j == n)
return true;
}
return false;
}
int main()
{
int n;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%s", s);
int len = strlen(s);
GetNext(s, len);
int k = Next[len];
while(k>len/)k=Next[k];
while(k>)
{
memset(ss, ,sizeof(ss));
strncpy(ss, s, k);///一定不要忘记初始化;
memset(sm, ,sizeof(sm));
strncpy(sm, s+k, len-*k);
if(kmp(ss, k, sm, len-*k)==true)
break;
else
k=Next[k];
}
printf("%d\n", k);
}
return ;
}

Theme Section---hdu4763(kmp, Next数组的运用)的更多相关文章

  1. HDU4763 Theme Section 【KMP】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. HDU 4763:Theme Section(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description   It's time for mus ...

  3. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  4. 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...

  5. 【HDU 4763】Theme Section(KMP)

    这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...

  6. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  7. Theme Section(KMP应用 HDU4763)

    Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)  ...

  9. hdu4763 Theme Section【next数组应用】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. hdu 4763 Theme Section(KMP水题)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. CentOS minimal 版安装图形界面的步骤分享,中文语言包

    1.连接网络: CentOS minimal.iso安装好后,进入终端,默认是不开网络的, 首先启用网卡, 自动获取ip. ifconfig eth0 up dhclient eth0 这时候再 if ...

  2. ThinkPHP 汉字转成多种形式拼音

    模型: <?php namespace Admin\Model; use Think\Model; /** * 汉字转拼音 * @author huangguojin */ class ZHMo ...

  3. MapReduce的InputFormat过程的学习

    转自:http://blog.csdn.net/androidlushangderen/article/details/41114259 昨天经过几个小时的学习,把MapReduce的第一个阶段的过程 ...

  4. 关于CSS 里的_width是什么意思???

    下划线_IE6支持下划线,IE7和firefox等均不支持下划线. 你那个代码的意思就是IE6下面宽度 449px;其他浏览器下宽度 460px; 友情提醒:这种HACK写法,得把_width写在正常 ...

  5. JSP 页面中用绝对路径显示图片

    首先,图片和工程不在一个盘符下.图片也不能放到工程下.  在JSP 文件中 <img src="E:/图片/1.jpg"/>  这样是引不到图片的.因为,JSP页面在引 ...

  6. React + Redux 入门(一):抛开 React 学 Redux

    http://www.hacke2.cn/think-in-react-redux-1/

  7. Git------MyEclipse中使用Git

    转载:http://www.mamicode.com/info-detail-928508.html

  8. 《转》武​汉​的​I​T​公​司

    本文转载自sherry020406前段时间看到版上有同学问在武汉找工作的情况,我谈谈去年找工作时碰到或者听到的一些企业,希望以下内容对想去武汉工作的同学有帮助,也算是对job版的回报.有些公司的情况可 ...

  9. handlebars Helper用法

    handlebars  Helper用法:  http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471357.html 逻辑运算符在handle ...

  10. 【BZOJ4275】[ONTAK2015]Badania naukowe DP

    [BZOJ4275][ONTAK2015]Badania naukowe Description 给定三个数字串A,B,C,请找到一个A,B的最长公共子序列,满足C是该子序列的子串. Input 第一 ...