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

Theme Section

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

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
 题意:求前中后最长相同串长度,不能有重叠,
我还以为我这种方法会超时,没想到过了。!!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#define INF 10000000
using namespace std;
char a[] , b[], str[];
int ans = ; void getnext(char *a , int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[j] == a[k])
{
k++;
j++;
next[j] = k ;
}
else
{
k = next[k];
}
}
} int main()
{
int n ;
scanf("%d" , &n);
while(n--)
{
int next[];
int next1[];
scanf("%s" , a);
int len = strlen(a);
getnext(a , len , next);
int q = next[len];
while(q > )
{
if(q * > len)
{
q = next[q];
continue ;
}
for(int i = ; i < q ; i++)
{
str[i] = a[i] ;
}
getnext(a , q , next1);
int j = , i = q ;
while(i < len - q && j < q)
{
if(j == - || str[j] == a[i])
{
i++;
j++;
}
else
{
j = next[j] ;
}
}
if(j == q)
{
ans = q;
break ;
}
q = next[q]; }
printf("%d\n" , q);
} return ;
}
 

最新文章

  1. iOS $299申请时碰到的狗血问题
  2. ActiveMQ 入门使用实例
  3. Model1模式的学生信息增删改查
  4. C# 三种实现抖屏的方式
  5. SELinux入门
  6. manacher算法_求最长回文子串长度
  7. Hadoop RPC源码阅读-客户端
  8. 【百度地图API】如何在地图上添加标注?——另有:坐标拾取工具+打车费用接口介绍
  9. Check for Palindromes-FCC
  10. GDAL书籍
  11. window下载android 最新源码
  12. Day3:html和css
  13. CSS有哪些属性是可以继承的?
  14. centos7搭建ELK Cluster集群日志分析平台(二):Logstash
  15. Educational Codeforces Round 42 (Rated for Div. 2) D. Merge Equals
  16. pymongo模块 目录
  17. NYOJ 一笔画
  18. 八 原型prototype和__proto__
  19. C# 委托、lambda表达式和事件
  20. Linux DRM KMS 驱动简介【转】

热门文章

  1. ajaxSubmit 实现图片上传 SSM maven
  2. java 判断点是否在一条线段上
  3. CentOS7搭建Flume与Kafka整合及基础操作与测试
  4. more 分页显示文件内容
  5. 解决 INSTALL FAILED CONFLICTING PROVIDER
  6. 三栏布局的三个典型方法(圣杯、双飞翼、flex)
  7. Django登录(含随机生成图片验证码)注册实例
  8. How to permanently set $PATH on Linux/Unix?
  9. LeetCode--044--通配符匹配(java)*
  10. Spring动态数据源-AbstractRoutingDataSource