HDU 4763 Theme Section ( KMP next函数应用 )
设串为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函数应用 )的更多相关文章
- hdu 4763 Theme Section(KMP水题)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 4763 Theme Section(KMP灵活应用)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4763 Theme Section
题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...
- 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...
- HDU - 4763 Theme Section (KMP的next数组的应用)
给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...
- CF126B password&&HDU 4763 Theme Section
http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...
- hdu 4763 Theme Section(next数组找串中三段相等)
题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...
随机推荐
- Python 二进制 八进制 十进制 十六进制
1.四种进制的表示方式 >>> 0b010 0b二进制 >>> 0x010 0x 十六进制 >>> 0o010 0o 八进制 >>&g ...
- spring-mybatis整合项目 异常处理2
org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/imooc ...
- nop 插件解析
在计算领域,插件( plug-in or plugin)是将特定的功能增加到大型软件中的软件组件. nopCommerce插件用来扩展nopCommerce的功能.nopCommerce 有几种插件. ...
- eBay 表结构
erp_ebay_list 建表语句 CREATE TABLE `erp_ebay_list` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `na ...
- 一件安装lnmp
wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-full.tar.gz && ...
- python之获取微信好友列表并保存文档中
代码如下 from wxpy import * from pprint import pprint #登录微信 bot = Bot() my_friend = bot.friends() f = op ...
- 使用selenium模拟登录知乎
网上流传着许多抓取知乎数据的代码,抓取它的数据有一个问题一定绕不过去,那就是模拟登录,今天我们就来聊聊知乎的模拟登录. 获取知乎内容的方法有两种,一种是使用request,想办法携带cookies等必 ...
- openwrt(二) 配置openwrt及编译
导航 1. 配置openwrt 2. 编译openwrt 3. 错误记录 1. 配置openwrt 在openwrt的根目录下,执行make menuconfig. 这个界面我也只是了解了这两个选项而 ...
- IIS发布错误记录
1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 BeginRequ ...
- spark练习---ip匹配以及广播的特性
今天,我们还是在介绍spark的小练习,这次的小练习还是基于IP相关的操作,我们可以先看一下今天的需求,我们有两个文件, 第一个文件,是IP的字典,也就是我们上一篇介绍过的,就是表明了所有IP字段所属 ...