设串为str, 串长为len。

对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式,然后就是在str[ans]-str[len-2*ans]中查找是不是包含E,找到就输出,找不到就沿着next向下寻找,缩短串长。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ; char str[MAXN];
char tmp[MAXN];
int nextval[MAXN];
int len; void GetNextVal( char *s, int lenth )
{
int i = , j = -;
nextval[] = -;
while ( i < lenth )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
if ( s[i] != s[j] ) nextval[i] = j;
else nextval[i] = nextval[j];
}
else j = nextval[j];
}
return;
} bool KMP( char *t, char *s, int lenth, int lenn )
{
//GetNextVal( t, lenth );
int i = , j = ;
while ( j < lenn )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenth ) return true;
}
else i = nextval[i];
//if ( i == lenth ) return true;
}
return false;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", str );
len = strlen(str);
GetNextVal( str, len );
int ans = nextval[len];
while ( ans > len/ ) ans = nextval[ans];
while ( ans > )
{
if ( KMP( str, &str[ans], ans, len - ans - ans ) )
break;
ans = nextval[ans];
}
if ( ans < ) printf("%d\n", len/);
else printf( "%d\n", ans );
}
return ;
}

HDU 4763 Theme Section ( KMP next函数应用 )的更多相关文章

  1. hdu 4763 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灵活应用)

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

  3. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  4. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

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

  5. HDU 4763 Theme Section

    题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...

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

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

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

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

  8. CF126B password&&HDU 4763 Theme Section

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...

  9. hdu 4763 Theme Section(next数组找串中三段相等)

    题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...

随机推荐

  1. 如何在linux服务器部署Rstudio server,配置ODBC远程访问win 服务器上的SQL server

    如何在linux服务器部署Rstudio server,配置ODBC后通过RODBC包远程访问SQL server 背景介绍:之前写过一篇文章,提到近期要部署Rstudio server(搭建数据分析 ...

  2. P2341 [HAOI2006]受欢迎的牛

    P2341 [HAOI2006]受欢迎的牛 塔尔羊标准模板(我才不会告诉你我嘴里含着一个九省联考的出题人) 不会劈配.林克卡特树.制胡窜 我还会叉粪宿主,梳妆素组,西安段素 #include<c ...

  3. java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数

    File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...

  4. java基础必备单词讲解 day three

    if 如果 else 否则 switch 切换判断 case 实例 break 退出 return 返回 default 默认 variable array 数组 null 空的 无效的 pointe ...

  5. 11-UITableView

    UITableView 掌握 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UITabl ...

  6. JNDI整理

    JNDI 什么是JNDI JNDI全称为Java Naming and Directory Interface,命名及目录查找接口,是java平台的一种标准扩展,它提供了一系列接口.类和命名空间的概念 ...

  7. SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别。

    DROP: DROP TABLE test; 删除表test,并释放空间,将test删除的一干二净. TRUNCATE: TRUNCATE test; 删除表test里的内容,并释放空间,但不删除表的 ...

  8. linux基础目录

    第1章 linux目录结构 1.1 linux目录结构的特点 一切皆文件 1)倒挂的树状结构   一切从根开始 2)linux每个目录可以挂载在不同的设备(磁盘)上.windows不容易做到. /da ...

  9. shell 脚本入门

    shell script 是一个程序化脚本,使用shell语法跟linux命令完成的一个程序. 注意事项: 文件开头: #!/bin/bash 文本以enter符号作为开始执行该行语句,用跳脱符 \e ...

  10. Shell学习——变量

    1.在Bash中,每一个变量的值都是字符串 2.查看某个进程的环境变量 cat /proc/$PID/environ | tr '\0' '\n' 3.变量赋值 3.1.var=value,注意var ...