题目网址: 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. Java基础集锦——利用Collections.sort方法对list排序

    要想对List进行排序,可以让实体对象实现Comparable接口,重写compareTo方法即可实现按某一属性排序,但是这种写法很单一,只能按照固定的一个属性排序,没变法变化.通过下面这种方法,可以 ...

  2. C# Use Pop3Client to read gmail

    host = "pop.gmail.com" user = "xxxxx@gmail.com" password = "xxxx" port ...

  3. 使用BAT批处理执行sql语句的代码

    使用BAT批处理执行sql语句的代码 有时候需要执行一些Sql语句时,不想开企业管理器,或者是发给客户执行但那边又不懂代码,这时就可以用下面方法 1.把待执行Sql保存在一个文件,这里为2011022 ...

  4. truncate/drop表非常慢,怎么办?用硬链接,极速体验

    这个这个,我必须花巨大篇幅,记录下今天清空表记录的英雄壮举,可知道一个drop操作,执行了一下午啊一下午,这是要急出翔的节奏..呵呵,下面开始 我的需求:某表因历史原因,积压了1亿条记录,约占360G ...

  5. NSURLSession与AFNetworking3.0

    下面是用GET方式请求一个页面数据的示例: AFNetworking 2.x NSString *siteUrl = @"http://webinar.ofweek.com/readDemo ...

  6. winedt设置自动显示行号[latex]

    options--preferences--appearance 在show line numbers for modes下面的文本框里添加;Tex 这样新建或者打开tex文件的时候就自动显示行号了( ...

  7. Android SDK开发包国内下载地址

    不知道是因为最近kaihui还是怎么的,打开android sdk官方网站特别的慢,想下载最新版本的platform几乎变成不可能完成的任务,不知道为什么Google不像Apache那样在各国设立镜像 ...

  8. 用SQL server导出到oracle,查询时提示“表或视图不存在ORA-00942”错误

    用SQL server2005的导出工具,将数据导出表到oracle,表名称里看到有这张表了,但查询或删除时都提示“ORA-00942表或者试图不存在”的错误,上网查了一下,是如下原因: “查询或删除 ...

  9. VisualSFM for Structure from Motion

    VisualSFM是Changchang Wu编写的使用 Structure from Motion (SfM)进行3D重建的交互界面,具体内容详见http://homes.cs.washington ...

  10. oracle 11g 如何创建、修改、删除list-list组合分区

    Oracle11g在分区方面做了很大的提高,不但新增了4种复合分区类型,还增加了虚拟列分区.系统分区.INTERVAL分区等功能. 9i开始,Oracle就包括了2种复合分区,RANGE-HASH和R ...