题目链接: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. 这不是bug,而是语言特性

    分析编程语言缺陷的一种方法是把所有的缺陷归于3类:不该做的做了,该做的没做,该做但做得不合适. 在使用switch case时,如果使用缺省的 fall through,请一定在旁边注释,因为97%的 ...

  2. python操作word之pywin32的安装

    PyCharm 2016.2 官网中文汉化破解版 注册码 http://idea.lanyus.com/ 首先下载安装win32com,下载32位的,不然安装的时候可能检测不到python https ...

  3. 解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题

    症状: Windows操作系统,eclipse开发环境下,在本机上使用http://localhost:8080/...访问本机上的页面,使用tomcat作为服务器 在Servlet或者Action中 ...

  4. Ubuntu之No module named cv2

    最简单的方法是:pip install opencv-python 另外,从源码安装的方法: 1下载opencv源码:http://opencv.org/releases.html  推荐2.4.13 ...

  5. Hadoop是原Yahoo的Doug Cutting根据Google发布的学术论文研究而来

    Hadoop是原Yahoo的Doug Cutting根据Google发布的学术论文研究而来.Doug Cutting给这个Project起了个名字,就叫Hadoop. Doug Cutting在Clo ...

  6. 为什么选择使用Spring Cloud而放弃了Dubbo

    为什么选择使用Spring Cloud而放弃了Dubbo 可能大家会问,为什么选择了使用Dubbo之后,而又选择全面使用Spring Cloud呢?其中有几个原因: 1)从两个公司的背景来谈:Dubb ...

  7. yii2 输出json的方法

    public function actionAjax() { if(isset(Yii::$app->request->post('test'))){ $test = "Ajax ...

  8. Angular2 HttpClient (一)

    @angular/common/http 中的 HttpClient 类为 Angular 应用程序提供了一个简化的 API 来实现 HTTP 客户端功能.它基于浏览器提供的 XMLHttpReque ...

  9. logback中MDC使用

    今天在项目发现别人写了很多MDC.put("taskid", "testThread/heart/main_heart");或者MDC.put("ta ...

  10. 简单是Jedis实例(相对连接池而言)

    在引入相关jar包后,只要new一个Jedis对象,就能做redis相关操作了.以下是一个简单的jedis实例: package com.pptv.redis; import java.util.Ar ...