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. 【剑指Offer学习】【面试题4 : 替换空格】

    题目: 请实现一个函数,把字符串中的每个空格替换成"%20",例如“We are happy.”,则输出“We%20are%20happy.”. 以下代码都是通过PHP代码实现. ...

  2. 利用最小二乘法拟合任意次函数曲线(C#)

    原文:利用最小二乘法拟合任意次函数曲线(C#) ///<summary>     ///用最小二乘法拟合二元多次曲线     ///</summary>     ///< ...

  3. net share列出了Windows的默认共享(包括C盘)

    另外还有单独开启办法: 开启共享方法: 命令行方式:net share 博客=F:\娱乐\种子 我设置了一个名为“博客”的共享,路径为:“F:\娱乐\种子”. GUI方式:找到“F:\娱乐”的“种子” ...

  4. WPF的逻辑树和视觉树

    原文:WPF的逻辑树和视觉树 这部分的内容来自于即将出版的新书<WPF Unleashed>的第三章样章.关于什么是逻辑树,我们先看下面的一个伪XAML代码的例子: <Window ...

  5. WPF下Itemscontrol分组 样式

    原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...

  6. 硬盘的结构和介绍,硬盘MBR详细介绍(超详细彩图)

    一.物理结构 1.磁道,扇区,柱面和磁头数 硬盘最基本的组成部分是由坚硬金属材料制成的涂以磁性介质的盘片,不同容量硬盘的盘片数不等.每个盘片有两面,都可记录信息.盘片被分成许多扇形的区域,每个区域叫一 ...

  7. Win10《芒果TV》商店内测版更新至v3.7.65.0:跨平台UI新体验,铺路SP

    Win10<芒果TV - Preview>是Win10<芒果TV>官方唯一指定内测预览版,最新的改进和功能更新将会在此版本优先体验. 春去夏来,初心犹在,Win10<芒果 ...

  8. VS点击调试卡住的问题解决方案(转载)

    本来今天好好的,不知道弄到了什么,调试不了了,一点击立马卡住,就一直在那转,就在网上找了找解决方案,下面给大家列出来几种可能会卡住的问题已经解决方案 1:加载调试符号引起的卡住 解决方案: 在“选项” ...

  9. 在Visual Studio 2017中找不到.NET Framework 4.6.2

    原文 https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/23/net-framwork-4-6-2-not-in-visual-stud ...

  10. shell中select、case的使用

    case和select结构在技术上说并不是循环, 因为它们并不对可执行代码块进行迭代. 但是和循环相似的是, 它们也依靠在代码块顶部或底部的条件判断来决定程序的分支. select   select结 ...