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. “TNS-03505:无法解析名称”问题解决一例

    1.  问题情境 开发人员,在windows新环境ORACLEclient.配置"tnsnames.ora"后,准备连接Linux环境的ORACLE数据库,使用tnsping报TN ...

  2. abp项目 从sql server迁移至mysql

    官方资料:https://aspnetboilerplate.com/Pages/Documents/EF-MySql-Integration 实验发现,还差了两步 整理一下,步骤如下: 1.引用My ...

  3. QuickReport根据每行的内容长度动态调整DetailBand1的行高

    procedure TPosPubFactureRep.DetailBand1BeforePrint(Sender: TQRCustomBand; var PrintBand: Boolean); v ...

  4. WPF用DirectSound播放声音

    示例代码: var fileName = @"D:\WindowsLogon.wav"; DevicesCollection sound_devices = new Devices ...

  5. Delphi 编写IC控件

    编写控件的基本步骤 1.确定一个祖先类 2.创建一个组件单元 3.在新控件中添加属性.方法和事件 事件定义方法如下: type private FOnClick:TNotifyEvent ;//( 声 ...

  6. 多玩YY语音的面试题:C++中如何在main()函数之前执行操作?

    多玩YY语音的面试题:C++中如何在main()函数之前执行操作? 第一反应main()函数是所有函数执行的开始.但是问题是main()函数执行之前如何执行呢? 联想到MFC里面的 C**App类的t ...

  7. 15 款 jQuery 社交分享插件

    过去几年中社交媒体越来越流行了,能够分享音乐.视频.图像甚至是其他的 docs 文档到互联网上去,这样子还能够提高页面的点击量.通常,一些社交媒体插件都能允许你的用户分享你网站上的内容到其他的社交平台 ...

  8. VC 函数调用的 汇编代码 浅析

    摘要:主要谈谈vc里面函数调用汇编成汇编代码的情形,首先针对之前的一个小程序,说说vc编译器的优化. 例子程序: #include <iostream>using namespace st ...

  9. 搭建RPC over HTTP 环境遇到的问题

    最近需要做RPC的IPS协议分析,需要了解一下RPC over HTTP的协议格式,由于此类数据包不易构造,故此想搭建一个抓一些包分析一下. 结果搭建这么个环境硬是用了我四个工作日的时间,崩溃加无语. ...

  10. poi 操作Excel 以及大数据量导出

    maven 依赖 (版本必须一致,否则使用SXSSFworkbook 时程序会报错) <dependency> <groupId>org.apache.poi</grou ...