题目网址: 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. Openvswitch原理与代码分析(8): 修改Openvswitch代码添加自定义action

    有时候我们需要自定义一些自己的action,根据包头里面的信息,做一些自己的操作.   例如添加一个action名为handle_example   第一.修改ofp-actions.c文件   首先 ...

  2. 第一篇HBuilder在这里!

    本文作为一个引子,所有相关文章,都会在这里做一个索引,现在还空着.谢谢

  3. Unity3d 在不同设备中的文件读写 的路径

    Application.dataPath : 数据路径   Unity Editor: <path tp project folder>/Assets Unity 编辑器:<工程文件 ...

  4. Log4Net简单使用

    一. Log4net是什么.优点 用来记录程序日志,优点:1.提供应用程序运行时的精确环境,可供开发人员尽快找到应用程序中的Bug:2.日志信息可以输出到不同的地方(数据库,文件,邮箱等). 二. L ...

  5. EvreryDay Collect

    1.在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService? 在System.Net中提供了一个NetworkCredential,只有获得该凭证的用户才能访问相 ...

  6. MS SQL的存储过程

    -- ============================================= -- Author: -- Create date: 2016-07-01 -- Descriptio ...

  7. iOS开发-NSDate使用

    时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面是iOS中时间戳 与 时间之间的转换方法: ...

  8. POJ 1013 Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36206   Accepted: 11 ...

  9. Cobbler批量安装Ubuntu/CentOS系统

    2013-07-25    一.安装和修改cobbler配置 1. Cobbler不在CentOS的基本源中,需要导入EPEL源升级软件包, 确保epel-release包的版本为最新,当前最新版本为 ...

  10. C 语言函数参数只能传指针,不能传数组

    今天被要求编写一个C/C++冒泡算法的程序,心想这还不是手到擒来的事儿,虽然最近都是用Javascript程序,很少写C/C++程序,但是好歹也用过那么多年的C语言: 首先想的是怎么让自己的代码看上去 ...