Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1999 Accepted Submission(s): 947
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
问给定字符串可否划分出三个相同子串 第一个以母串首字符开始 最后一个以母串结尾字符结束 中间的在母串中 三个串不可重叠
相同子串 能联想到KMP的前缀方法(从当前字符的next数组指向的位置往前到母串首 和 从当前字符向前相同长度的串是相同串)
三串不好想 如果两串呢
首尾找最长相同两串我的思路是找出最后一个字母不断next过程中经过的下标 下标>母串长len/2 的话肯定不行 <len/2 的最大的就是所求长度(一定满足 因为是从母串中心分开的 必定不重叠
三串可以用同样思路 找小于len/3的 这样前后都满足了相同且不重叠 但中间串呢 上STL吧……或者用自己做好的KMP也行 图省事。。只要找到当前坐标往前的串在当前坐标往后的串中第一次出现的位置 看是否在当前坐标x*2~len-x之间即可
代码如下:
#include <bits/stdc++.h> using namespace std; char str[1111111]; int Next[1111111]; void GetNext() { Next[0] = -1; int k,len; len = strlen(str); for(int i = 1; i < len; ++i) { k = Next[i-1]; while(k != -1 && str[k+1] != str[i]) k = Next[k]; if(k == -1 && str[0] != str[i]) Next[i] = -1; else if(k == -1 && str[0] == str[i]) Next[i] = 0; else Next[i] = k+1; } } int num[1111111],tp; int main() { int n,m,t,i,j,u,v,w,len,k,mm,mlen,l,r; int pos; scanf("%d",&t); while(t--) { scanf("%s",str); GetNext(); len = strlen(str); tp = 0; num[tp++] = len; k = Next[len-1]; while(k != -1) { num[tp++] = k+1; k = Next[k]; } sort(num,num+tp); mm = -1; for(i = 0; num[i] <= len/3; ++i) { if(mm == -1 || num[mm] < num[i]) mm = i; } while(mm != -1) { l = strstr(str+num[mm],str+len-num[mm])-str; if(l >= num[mm] && l+num[mm] <= len-num[mm]) break; --mm; } if(mm == -1) puts("0"); else printf("%d\n",num[mm]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。