题目链接:https://vjudge.net/problem/HDU-4763

Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4264    Accepted Submission(s): 2056

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
 
Source
 
Recommend
liuyiding

题解:

给定一个字符串,找出能构成"EAEBE"形式的字符串的E的最长长度。其中A和B任意。

1.可知next[len]就是:即是前缀又是后缀的最长子串。所以我们就解决的首尾两个E(如果大于三分之一长度,可以通过next数组回退)。

2.剩下的问题就是:在两个E之间的连续子串中,是否能找到第三个E。

3.怎么找到?枚举里面的每个位置(注意不能与首尾的E有重叠),通过next数组的回退,一直找。详情还是看代码吧。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", x);
int len = strlen(x);
get_next(x, len);
int r = Next[len];
while(r> && len/r<) r = Next[r]; bool hav_ans = false;
for(; Next[r]>=; r = Next[r])
{
for(int i = *r; i<=len-r; i++)
{
int k = i; //不要写成Next[i];
while(Next[k]>r) k = Next[k];
if(Next[k]==r)
{
hav_ans = true;
break;
}
}
if(hav_ans) break;
}
printf("%d\n", r);
}
}

最新文章

  1. Android与H5交互
  2. Java文件拷贝
  3. IIS7 应用程序池设置成 经典 v2.0
  4. net 连mysql奇怪问题
  5. linux 实时时钟(RTC)驱动【转】
  6. 几种不同风格的Toast
  7. javascript 浏览器执行断点
  8. 【C语言】字符集和词汇
  9. RSA 加密
  10. ElasticSearch 学习记录之ES短语匹配基本用法
  11. GA:利用GA对一元函数进行优化过程,求x∈(0,10)中y的最大值——Jason niu
  12. 从0到1用eclipse用maven搭建web项目
  13. angular,vue,react的基本语法—动态属性、事件绑定、ref,angular组件创建方式
  14. 一些Android手机的平台信息
  15. 自学Linux Shell14.3-创建临时文件
  16. java.util.ConcurrentModificationException的解决办法
  17. 关于DDL、DML和DCL的区别与理解
  18. Maven入门-5.Maven的聚合和继承
  19. Jmete ----r默认报告优化
  20. nw

热门文章

  1. ifame标签
  2. 家用电脑架服务器提供web
  3. AtCoder Regular Contest 074F - Lotus Leaves
  4. MarsEdit 快速插入代码
  5. android 完美退出应用程序。
  6. PAT (Advanced Level) 1086. Tree Traversals Again (25)
  7. HSSF生成excel文件损坏
  8. Java生成读取条形码和二维码图片
  9. iOS Block学习
  10. Android中怎样自己制作su