ex_KMP--Theme Section
题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/B
Description
To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?
Input
Output
Sample Input
xy
abc
aaa
aaaaba
aaxoaaaaa
Sample Output
0
1
1
2
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- char s[];
- int nex[];
- int pre[];
- void ex_next(int length)
- {
- ///nex[i]: 以第i位置开始的子串与T的前缀的最大长度;
- int i,j;
- nex[]=length;
- for(i=; i<length-&&s[i]==s[i+];i++);///前缀都是同一个字母的时候;
- nex[]=i;
- int a=;///a为使匹配到最远的地方时的起始匹配地点;
- for(int k=;k<length;k++)
- {
- int p=a+nex[a]-,L=nex[k-a];
- if( (k-)+L>=p )
- {
- int j=(p-k+)>?(p-k+):;
- while(k+j<length&&s[k+j]==s[j]) j++;
- /// 枚举(p+1,length) 与(p-k+1,length) 区间比较;
- nex[k]=j,a=k;
- }
- else nex[k]=L;
- }
- }
- int main()
- {
- int T,M,n;
- scanf("%d",&T);
- while(T--)
- {
- M=;
- scanf("%s",s);
- int len=strlen(s);
- ex_next(len);
- for(int i=;i<len;i++)
- {
- if(nex[i])
- {
- ///此时nex[i]为中间以s[i]开始的子串与前缀匹配的最大长度;
- ///接下来判断后缀与前缀及中间子串的最大匹配长度;
- n=min(min (i,nex[i]),(len-i)/);
- for(int j=len-n;j<len;j++)
- if(nex[j]==len-j)
- {
- if(M<nex[j]) M=nex[j];
- else break;
- }
- }
- }
- printf("%d\n",M);
- }
- return ;
- }
ex_KMP--Theme Section的更多相关文章
- Theme Section(KMP应用 HDU4763)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 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
题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...
- HDU 4763 Theme Section(KMP灵活应用)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu4763 Theme Section【next数组应用】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763
http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) ...
- hdu4763 Theme Section
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...
- HDU4763 Theme Section —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU4763 Theme Section 【KMP】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- Openvswitch原理与代码分析(8): 修改Openvswitch代码添加自定义action
有时候我们需要自定义一些自己的action,根据包头里面的信息,做一些自己的操作. 例如添加一个action名为handle_example 第一.修改ofp-actions.c文件 首先 ...
- 第一篇HBuilder在这里!
本文作为一个引子,所有相关文章,都会在这里做一个索引,现在还空着.谢谢
- Unity3d 在不同设备中的文件读写 的路径
Application.dataPath : 数据路径 Unity Editor: <path tp project folder>/Assets Unity 编辑器:<工程文件 ...
- Log4Net简单使用
一. Log4net是什么.优点 用来记录程序日志,优点:1.提供应用程序运行时的精确环境,可供开发人员尽快找到应用程序中的Bug:2.日志信息可以输出到不同的地方(数据库,文件,邮箱等). 二. L ...
- EvreryDay Collect
1.在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService? 在System.Net中提供了一个NetworkCredential,只有获得该凭证的用户才能访问相 ...
- MS SQL的存储过程
-- ============================================= -- Author: -- Create date: 2016-07-01 -- Descriptio ...
- iOS开发-NSDate使用
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面是iOS中时间戳 与 时间之间的转换方法: ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- Cobbler批量安装Ubuntu/CentOS系统
2013-07-25 一.安装和修改cobbler配置 1. Cobbler不在CentOS的基本源中,需要导入EPEL源升级软件包, 确保epel-release包的版本为最新,当前最新版本为 ...
- C 语言函数参数只能传指针,不能传数组
今天被要求编写一个C/C++冒泡算法的程序,心想这还不是手到擒来的事儿,虽然最近都是用Javascript程序,很少写C/C++程序,但是好歹也用过那么多年的C语言: 首先想的是怎么让自己的代码看上去 ...