http://acm.hdu.edu.cn/showproblem.php?pid=4763

Theme Section

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
 
题意:一条串可以分成前中后三个部分,求三个部分最长的相同子串的长度。
思路:做了好久的KMP一直只会模板题,坚持不看题解,终于花了半天时间搞出这道偏水的题目,主要在于 中间的部分 的定义,该串不包含前面,也不包含后面的部分就是中间,理解错题意瞎搞了好久。我的做法是先劈成三部分,然后先对前后用kmp算最长的公共前后缀长度,然后再把前后缀部分保留,其他全部给中间部分,然后分别再对前面和中间,后面和中间用kmp算出最长的公共子串长度,然后取较小者即可。算是一个比较暴力无脑的做法吧= =。
 

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define N 1000005
string s;
string le, ri, mi, temp;
int nxt[N]; void make_next(string s)
{
memset(nxt, , sizeof(nxt));
int l = s.size();
int j = -, i = ;
nxt[] = -;
while(i < l) {
if(j == - || s[j] == s[i]) {
j++; i++;
nxt[i] = j;
} else {
j = nxt[j];
}
}
}
/*
前面的串和后面的串匹配的时候只能前后缀匹配传入0
前面的串和中间的串匹配或者后面的串和中间的串匹配传入1
*/
int kmp(string s, string str, int flag)
{
int l = s.size(), L = str.size();
make_next(s);
int i = , j = ;
while(i < L && j < l) {
if(j == - || s[j] == str[i]) {
i++; j++;
if(flag && j==l) return j;
} else {
j = nxt[j];
}
}
if(i == L) return j;
return ;
} int main()
{
int t;
cin >> t;
while(t--) {
s.clear();
temp.clear();
mi.clear();
le.clear();
ri.clear();
cin >> s;
int len = s.size();
if(len < ) {
puts(""); continue;
}
for(int i = ; i < len; i++) {
if(i <= len/ - ) {
le += s[i];
} else if(i >= len*/) {
ri += s[i];
} else {
mi += s[i];
}
}//将一条串拆成三部分 int lo = kmp(le, ri, );
if(lo == ) {
puts(""); continue;
}
//lo是公共前后缀长度
// }
for(int i = ; i < ri.size()-lo; i++)
mi += ri[i]; for(int i = lo; i < le.size(); i++) {
temp += le[i];
} int l = le.size(), r = ri.size(); ri.erase(, r - lo);
le.erase(lo, r);
temp += mi; int ans = ;
int k = kmp(le, temp, );
int g = kmp(ri, temp, );
ans = min(k, g); cout << ans << endl;
}
return ;
}

HDU 4763:Theme Section(KMP)的更多相关文章

  1. 【HDU 4763】Theme Section(KMP)

    这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...

  2. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 4635:Strongly connected(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给出n个点和m条边,问最多能添加几条边使得图不是一个强连通图.如果一开始强连通就-1.思路:把图分成 ...

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

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

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

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

  6. HDU 3746:Cyclic Nacklace(KMP循环节)

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

  7. HDU 1711:Number Sequence(KMP模板,求位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 1890:Robotic Sort(Splay)

    http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的 ...

  9. HDU 2767:Proving Equivalences(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...

随机推荐

  1. Bootstrap按钮组 按钮工具栏 嵌套

    @{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport&q ...

  2. vs2015 cordova环境安装【个人遇到的几个问题】

    原文:vs2015 cordova环境安装[个人遇到的几个问题] 问题1: vs2015,设置  Debug  Android 设备[真机调试] Exception in thread "m ...

  3. Central Subscriber Model Explained

    原文 http://www.sqlrepl.com/sql-server/central-subscriber-model-explained/ The majority of SQL Server ...

  4. Android手机导出文件

    因为要写联系人相关的东西,所以得把db导出来看看 第一步:Root手机 尝试了几个Root工具,发现就KingRoot能root 第二个:编写bat脚本 脚本内容是先将DB文件从/data/data ...

  5. WCF nginx反向代理遇到的问题

    正常配置了nginx反向代理,其他java站点什么的都正常,就wcf总是失败.始终会跑如下异常: 由于 AddressFilter 在 EndpointDispatcher 不匹配,To 为“http ...

  6. .NET解析xml字符串,通过反射给实体类对象赋值,获取实体类数据列表

    /// <summary> /// 解析xml字符串 转换为实体类列表数据 /// </summary> /// <param name="xmlStr&quo ...

  7. jQuery简明教程

    本文参考w3cshool中文教程,网址:http://www.w3school.com.cn/jquery/index.asp 简介 jQuery是一个Javascript库,使用其的主要目的是简化J ...

  8. stdlib.h,string.h,wchar.h的函数列表(cplusplus.com就有,很清楚)goodx

    Multibyte characters mblen Get length of multibyte character (function ) mbtowc Convert multibyte se ...

  9. Zabbix监控ActiveMQ

    当我们在线上使用了ActiveMQ 后,我们需要对一些参数进行监控,比如 消息是否有阻塞,哪个消息队列阻塞了,总的消息数是多少等等.下面我们就通过 Zabbix 结合 Python 脚本来实现对 Ac ...

  10. 利用Maven, 搭建最简单的SpringMVC框架

    本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手. 文章下载