题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/B

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
 
题意: 输入一个字符串,求这个字符串中的一个子串,该子串为此字符串的前缀和后缀,并在字符串中间也有这个子串,但但这三个子串不能重叠,求是否存在这样的子串,输出子串长度的最大值,否则输出0;
 
思路: 使用扩展KMP算法,扩展KMP的next[i]表示从s[i]开始的与s前缀最大的匹配长度。在一个位置i中,如果要满足要求,子串的最大长度不能超过
min(i,  next[i], (len - i) / 2) ,所有的最大长度的最大值就是可能存在的最大长度。先找利用next[]找中间与前缀匹配的子串,然后判断后缀与子串匹配的最大长度,并要注意子串不能重叠。
 
代码:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. using namespace std;
  6. char s[];
  7. int nex[];
  8. int pre[];
  9.  
  10. void ex_next(int length)
  11. {
  12. ///nex[i]: 以第i位置开始的子串与T的前缀的最大长度;
  13. int i,j;
  14. nex[]=length;
  15. for(i=; i<length-&&s[i]==s[i+];i++);///前缀都是同一个字母的时候;
  16. nex[]=i;
  17. int a=;///a为使匹配到最远的地方时的起始匹配地点;
  18. for(int k=;k<length;k++)
  19. {
  20. int p=a+nex[a]-,L=nex[k-a];
  21. if( (k-)+L>=p )
  22. {
  23. int j=(p-k+)>?(p-k+):;
  24. while(k+j<length&&s[k+j]==s[j]) j++;
  25. /// 枚举(p+1,length) 与(p-k+1,length) 区间比较;
  26. nex[k]=j,a=k;
  27. }
  28. else nex[k]=L;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int T,M,n;
  35. scanf("%d",&T);
  36. while(T--)
  37. {
  38. M=;
  39. scanf("%s",s);
  40. int len=strlen(s);
  41. ex_next(len);
  42. for(int i=;i<len;i++)
  43. {
  44. if(nex[i])
  45. {
  46. ///此时nex[i]为中间以s[i]开始的子串与前缀匹配的最大长度;
  47. ///接下来判断后缀与前缀及中间子串的最大匹配长度;
  48. n=min(min (i,nex[i]),(len-i)/);
  49. for(int j=len-n;j<len;j++)
  50. if(nex[j]==len-j)
  51. {
  52. if(M<nex[j]) M=nex[j];
  53. else break;
  54. }
  55. }
  56. }
  57. printf("%d\n",M);
  58. }
  59. return ;
  60. }
 

ex_KMP--Theme Section的更多相关文章

  1. Theme Section(KMP应用 HDU4763)

    Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. hdu 4763 Theme Section(KMP水题)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. HDU 4763 Theme Section

    题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...

  4. HDU 4763 Theme Section(KMP灵活应用)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. hdu4763 Theme Section【next数组应用】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)  ...

  8. hdu4763 Theme Section

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...

  9. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  10. HDU4763 Theme Section 【KMP】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. checkbox与说明文字无法对齐的问题

    解决方法: vertical-align:middle; 例:<input type=checkbox id="theId" name=checkbox style=&quo ...

  2. iOS 语言切换、本地化,国际化

    什么是本地化处理? 本地化处理就是我们的应用程序有可能发布到世界的很多国家去,因为每个国家应用的语言是不一样的,所以我们要把我们的应用程序的语言要进行本地化处理一下. 本地化处理需要处理那些文件? ( ...

  3. spring 集成 Hibernate4.3.X org.hibernate.service.jta.platform.spi.JtaPlatform异常

    使用Spring3.2.4集成Hibernate4.3.5时,出现以下异常 Causedby:java.lang.ClassNotFoundException:org.hibernate.servic ...

  4. android开发(49) android 使用 CollapsingToolbarLayout ,可折叠的顶部导航栏

    概述 在很app上都见过 可折叠的顶部导航栏效果.google support v7  提供了 CollapsingToolbarLayout 可以实现这个效果.效果图如下:    实现步骤 1. 写 ...

  5. ZooKeeper与Curator注册和监控

    Curator提供了对zookeeper客户端的封装,并监控连接状态和会话session,特别是会话session过期后,curator能够重新连接zookeeper,并且创建一个新的session. ...

  6. C#连接mysql数据库插入数据后获取自增长主键ID值

    From: http://blog.csdn.net/zbc496218/article/details/51082983 MySqlConnection conn = new MySqlConnec ...

  7. URAL 1549 Another Japanese Puzzle(构造)

    题目大意 构造一条闭合路线,使得路线不能相交,并且走直线的步数小于等于 S,转弯(左转和右转)的步数小于等于 T.(0≤S,T≤1000) 求一条最长的路线 做法分析 注意到,因为要求路线闭合,那么转 ...

  8. Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1)

    Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1) To Bottom Modified:03-May-2013T ...

  9. ASP.NET MVC 4 Web编程

    http://spu.jd.com/11309606.html 第1章 入门第2章 控制器第3章 视图第4章 模型第5章 表单和HTML辅助方法第6章 数据注解和验证第7章 成员资格.授权和安全性第8 ...

  10. SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework

    1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...