题目来源:http://codeforces.com/contest/127/problem/D
</pre><span style="font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif;font-size:12px; line-height: 24px;">就是求最大前缀 后缀 并且要求在中间出现过</span></p><p><span style="font-family:Microsoft YaHei, 微软雅黑, Arial, Lucida Grande, Tahoma, sans-serif;font-size:12px;"><span style="line-height: 24px;"><span style="font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif;font-size:12px; line-height: 24px;"></span></span></span></p><p><span style="font-family:Microsoft YaHei, 微软雅黑, Arial, Lucida Grande, Tahoma, sans-serif;font-size:12px;"><span style="line-height: 24px;">解释在代码中:</span></span></p><p><span style="font-family:Microsoft YaHei, 微软雅黑, Arial, Lucida Grande, Tahoma, sans-serif;font-size:12px;"><span style="line-height: 24px;"></span></span></p><p><span style="font-family:Microsoft YaHei, 微软雅黑, Arial, Lucida Grande, Tahoma, sans-serif;font-size:12px;"><span style="line-height: 24px;"></span></span><pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000100
int next[N],len,vis[N];
char a[N];
void getfail(char *a)
{
int i,j;
next[0]=-1;
i=0;
j=-1;
while(i<len)
{
if(j==-1||a[i]==a[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int i,j;
while(~scanf("%s",a))
{
len=strlen(a);
getfail(a);
for(i=0;i<=next[len];i++)
vis[i]=0; //标记出现过的前缀
for(i=1;i<len;i++)
vis[next[i]]=1;
i=next[len];
int flag=0;
while(i) //这一步应该是重点,为什么跳到next[i]?
//因为next[i],记录的是最大前缀,当这个前缀没有在中间出现过
//,而你只能在当前最大前缀中找次最长后缀等于前缀所以长度就是缩短为next[i]了
{
if(vis[i])
{
flag=1;
a[i]='\0';
break;
}
i=next[i];
}
if(!flag)
printf("Just a legend\n");
else
printf("%s\n",a);
}
return 0;
}