http://codeforces.com/problemset/problem/127/D
题意:求一个字符串S的最长子串,使得它同时是S的前缀,后缀和中缀。
思路:这道题有多种方法,这里介绍SAM的方法,我们先构造S的SAM,对于SAM的每一个状态,设num为其所代表字串在S中出现的个数,r为其代表子串在S中出现的最右位置(这两个量由SAM的性质可以拓扑排序后自底向上求出)。然后用S在SAM上做匹配。假设我们已经匹配了长度n,由于当前匹配的串一定是S的前缀,我们只要判定它是否为中缀和后缀,什么样的串是S的后缀呢?显然是在S中出现位置为|S|的子串,则假若当前走到状态p,则若p->r==|S|,说明它一定是S的后缀,判定是否为中缀只要看它在S中出现次数大于3即可。则我们一次匹配后,最后一个满足前两个要求的子串即为所求,若没有则输出没有。代码如下:
#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 2000010
#define Smaxn 26
#define inf 21000000
using namespace std;
struct node
{
node *par,*go[Smaxn];
int right;
int num;
int po;
int val;
}*root,*tail,que[maxn],*top[maxn];
int tot;
char str[maxn>>1];
void add(int c,int l,int po)
{
node *p=tail,*np=&que[tot++];
np->val=l;
np->po=po;
while(p&&p->go[c]==NULL)
p->go[c]=np,p=p->par;
if(p==NULL) np->par=root;
else
{
node *q=p->go[c];
if(p->val+1==q->val) np->par=q;
else
{
node *nq=&que[tot++];
*nq=*q;
nq->val=p->val+1;
np->par=q->par=nq;
while(p&&p->go[c]==q) p->go[c]=nq,p=p->par;
}
}
tail=np;
}
int c[maxn],len;
void init()
{
memset(que,0,sizeof(que));
tot=0;
len=1;
root=tail=&que[tot++];
}
void solve(int n)
{
memset(c,0,sizeof(c));
int i;
for(i=0;i<tot;i++)
c[que[i].val]++;
for(i=1;i<len;i++)
c[i]+=c[i-1];
for(i=0;i<tot;i++)
top[--c[que[i].val]]=&que[i];
for(node *p=root;;p=p->go[str[p->val+1]-'a'])
{
p->num=1;
p->right=p->po;
//printf("f");
if (p->val==len-1)break;
}
for(i=tot-1;i>=0;i--)
{
node *p=top[i];
if(p->right==0)
{
p->right=p->po;
}
if(p->par)
{
node *q=p->par;
q->num+=p->num;
if(q->right==0||q->right<p->right)
q->right=p->right;
}
}
int po=0;
node *p=root;
for(i=1;i<=n;i++)
{
p=p->go[str[i]-'a'];
if(str[i]==str[n])
{
if(p->right==n&&p->num>=3)
po=i;
}
}
if(po==0)
printf("Just a legend\n");
else
{
for(i=1;i<=po;i++)
printf("%c",str[i]);
printf("\n");
}
}
int main()
{
freopen("dd.txt","r",stdin);
scanf("%s",str+1);
int i,l=strlen(str+1);
init();
for(i=1;i<=l;i++)
{
add(str[i]-'a',len++,i);
}
solve(l);
return 0;
}