Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1098    Accepted Submission(s): 570

Problem Description
It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the
rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of 'EAEBE', where
section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.



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
The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.
 
Output
There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
 
Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
 
Sample Output
0
0
1
1
2
 
题意:输出最长的前缀, 前缀要满足在原串中至少匹配3次

思路: 二分前缀的结尾点。右边为3分之中的一个原串,用KMP 统计出现次数。就可以

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000000+10;
int next[maxn],n;
char str[maxn]; void getNext(){
next[0] = next[1] = 0;
for(int i = 1,j; i < n; i++){
j = next[i];
while(j && str[i] != str[j]) j = next[j];
if(str[i] == str[j]) next[i+1] = j+1;
else next[i+1] = 0;
}
} bool find(int ed){
int cnt = 0;
for(int i = 0,j = 0; i < n; i++){
while(j && str[i] != str[j]) j = next[j];
if(str[i]==str[j]) j++;
if(j==ed){
++cnt;
j = 0;
if(cnt>=3) return true;
}
}
return false;
} int binary_search(){
int L = 1,R = n/3+1,mid;
while(L <= R){
mid = (L+R)>>1;
if(find(mid)){
L = mid+1;
}else{
R = mid-1;
}
}
if(find(L)) return L;
else if(find(R)) return R;
else return L-1; } int main(){ int ncase;
cin >> ncase;
while(ncase--){
scanf("%s",str);
n = strlen(str);
getNext();
printf("%d\n",binary_search());
}
return 0;
}

HDU4763-Theme Section(KMP+二分)的更多相关文章

  1. HDU4763 Theme Section —— KMP next数组

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

  2. HDU4763 - Theme Section(KMP)

    题目描述 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 CF原题不解释....http://codeforces.com/probl ...

  3. HDU-4763 Theme Section KMP

    题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...

  4. Theme Section(KMP应用 HDU4763)

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

  5. HDU4763 Theme Section 【KMP】

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

  6. hdu 4763 Theme Section(KMP水题)

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

  7. HDU 4763 Theme Section(KMP灵活应用)

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

  8. hdu4763 Theme Section

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...

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

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

  10. 【kmp算法】hdu4763 Theme Section

    kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...

随机推荐

  1. vs2013+ffmpeg开发环境搭建【转】

    本文转载自:http://blog.csdn.net/spaceyqy/article/details/43115391 每当看到配环境,我就泪流满面,好吧,闲话不多说,进入正题. 1.去官方下载ff ...

  2. python spark 求解最大 最小 平均 中位数

    rating_data_raw = sc.textFile("%s/ml-100k/u.data" % PATH) print rating_data_raw.first() nu ...

  3. 最详细的CentOS 6与7对比(一):常见设置对比

    本主题将从3个角度进行对比 常见设置(CentOS 6 vs CentOS 7) 服务管理(Sysvinit vs Upstart vs Systemd) 性能测试(cpu/mem/io/oltp) ...

  4. getElementById和querySelector区别

    1.常见的获取元素的方法有3种,分别是通过元素ID document.getElementById('idName');.通过标签名字document.getElementsByTagName(tag ...

  5. OC数组和字典中存入niu值

    在NSArray和NSDictionary中nil有特殊的含义.但是某些时候,我们必须要放入nil怎么办? 要想放入nil就必须用到一个类NSNull,这个类只有一个类方法,就是null.[NSNul ...

  6. caffe 参数介绍 solver.prototxt

    转载自 http://blog.csdn.net/cyh_24/article/details/51537709 solver.prototxt net: "models/bvlc_alex ...

  7. Django学习案例一(blog):一. 创建project、app

    1.创建project 方法1:使用命令行创建项目.在E盘cmd执行如下命令: django-admin.py startproject myblog 方法2:使用pycharm创建项目.放置位置为D ...

  8. VM虚拟机NAT模式主机与虚拟机ping不通解决方案

    VM虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST-ONLY 模式. NAT模式 使用虚拟机的一个虚拟网卡做NAT网关,在nat网关上配dhcp ,或者直接用静态地址.就相当于形成了一 ...

  9. 部署Seafile服务

    介绍 官网:https://www.seafile.com 客户端/服务端下载:https://www.seafile.com/download/ 中文安装教程(MySQL版):http://manu ...

  10. DP:***24种设计模式--转自刘伟

    转自于高人的文章:http://blog.csdn.net/lovelion/article/details/17517213 2012年-2013年,Sunny在CSDN技术博客中陆续发表了100多 ...