Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230121
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 848
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 10:27
文章分类
文章存档

2014年(9)

2013年(66)

我的朋友

分类: C/C++

2013-10-12 16:24:00

Theme Section
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 574    Accepted Submission(s): 308




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
2013 ACM/ICPC Asia Regional Changchun Online
 


Recommend
liuyiding
 
题意:
给你一个字符串。要你找出最长的子串长度。该子串需满足。在串的开头和中间和结尾分别出现一次,且不能交叉。
思路:
很简单的KMP。关键在于KMP思维的转化。思维很重要!先用文本串做个失配数组。设文本串长度为len。串从0开始编号。
我们考虑匹配第len个位置。失配数组每跳一个位置。都能保证文本串的开头位置匹配。所以只需在剩下的中间部分找有没有匹配的子串。如果有说明满足条件。
详细见代码:
[cpp] view plaincopy
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
using namespace std;  
const int INF=0x3f3f3f3f;  
const int maxn=1000010;  
int f[maxn];  
char txt[maxn];  
void getf(char *p)  
{  
    int i,j,n=strlen(p);  
    f[0]=f[1]=0;  
    for(i=1;i     {  
        j=f[i];  
        while(j&&p[j]!=p[i])  
            j=f[j];  
        f[i+1]=p[j]==p[i]?j+1:0;  
    }  
}  
bool kmp(char *T,char *p,int n,int m)  
{  
    int i,j;  
    for(i=0,j=0;i     {  
        while(j&&p[j]!=T[i])  
            j=f[j];  
        if(p[j]==T[i])  
            j++;  
        if(j==m)  
            return true;  
    }  
    return false;  
}  
int main()  
{  
    int t,j,ans,len;  
  
    scanf("%d",&t);  
    while(t--)  
    {  
        ans=0;  
        scanf("%s",txt);  
        len=strlen(txt);  
        getf(txt);  
        j=f[len];  
        while(j)  
        {  
            if(len>=3*j&&kmp(txt+j,txt,len-2*j,j))  
            {  
                ans=j;//j即为长度  
                break;  
            }  
            j=f[j];  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
}  
阅读(1007) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~