Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.

A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s.

Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.

Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened.

You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend.

Input

You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.

Output

Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes.

Example

Input
fixprefixsuffix
Output
fix
Input
abcdabc
Output
Just a legend
完全没有想法。先开始脑抽了,想了个二分,很激动的认为对了,结果wa,然后发现公共前缀的公共后缀的长度似乎不能二分。。。gg
又学了一下kmp。。。之前学的忘光了。。。
还是不太懂(mark)
这道题巧妙地利用了kmp中的nxt,这里的nxt就是kmp中预处理的失配函数。nxt[i]表示的是[1-i]位置的字符串中最长的前缀和后缀的公共部分的长度(s=abcab的nxt[5]=2,因为s[1-2]=s[4-5])
我们要求的正好和nxt所表示的东西很相似,于是就用上。
求完nxt,把每个长度标记。nxt[n]不标记,因为这是开头和结尾,不能算作中间的最长长度
从nxt[n](n是字符串长度)开始,表示的是整个串的前后缀最长公共部分的长度,如果这个长度存在,那么就可以了。(因为这个是最大的nxt,nxt是递减的)
挖个坑,似乎还是不是很理解
似乎理解了,就是公共长度不断缩小,但是kmp。。。
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000010
int n;
int nxt[N],used[N];
char s[N];
void Init()
{
int j=;
for(int i=;i<=n;i++)
{
while(j&&s[i]!=s[j+]) j=nxt[j];
if(s[i]==s[j+]) j++;
nxt[i]=j;
}
}
int main()
{
scanf("%s",s+);
n=strlen(s+);
Init();
for(int i=;i<n;i++) used[nxt[i]]=;
used[]=;
for(int i=n;i;i=nxt[i]) if(used[nxt[i]])
{
for(int j=;j<=nxt[i];j++) printf("%c",s[j]);
return ;
}
printf("Just a legend");
return ;
}

codeforces 126B的更多相关文章

  1. Codeforces 126B. Password(KMP,DP)

    Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...

  2. CodeForces 126B Password

    题目链接:http://codeforces.com/problemset/problem/126/B 题目大意: 多组数据每组给定1个字符串S,问是否存在S的一个尽量长的子串,同时是S的前缀和后缀, ...

  3. Codeforces 126B. Password (KMP)

    <题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...

  4. Codeforces 126B(kmp)

    要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...

  5. 【Codeforces 126B】Password

    [链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串 ...

  6. Codeforces 126B Password(Z算法)

    题意 给定一个字符串 \(s\) ,求一个子串 \(t\) 满足 \(t\) 是 \(s\) 的前缀.后缀且在除前缀后缀之外的地方出现过. \(1 \leq |s| \leq 10^6\) 思路 \( ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. spring boot 添加mybatis,以及相关配置

    首先在pom.xml文件里加入 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifa ...

  2. C#语言 数组

  3. iOS之UI--涂鸦画板实例

     iOS之UI--涂鸦画板实例  首先是搭建框架 其他的略过,直接展示效果: 然后接下来上传搭建好两个控制器框架的源码百度云下载链接: http://pan.baidu.com/s/1skjpDox  ...

  4. Python调用C/Fortran混合的动态链接库--中篇

    接下来,介绍一个简单的例子,从fortran中传递并返回一维自定义结构体数组到python注意点:1.fortran新标准支持可分配数组作为变量传入并在subroutine或function分配后返回 ...

  5. WPF代码注意事项,开发常见问题,知识总结

    代码注意事项: 1.代码实现的样式赋值 XXX.Style = TryFindResource("StyleName") as Style; 2.WPF中FindName方法的使用 ...

  6. C++的string连接(a = a + b 与 a += b)

    大一学习C语言的时候,书上就写着a = a + b与 a += b等价,但是提倡用后者. 在CSDN上也看到一个关于a+=b和a=a+b的区别的帖子,大概内容如下:------------------ ...

  7. DRF 之 版本控制

    1.版本控制是干什么用的,为什么要有版本? 我们都知道每一个程序都是有版本的.而且版本也会升级从v1升级到v2.v3.v4·····,但是我们不可能因为新版本出现旧版本就不去维护,因为用户有权选择不更 ...

  8. A nonrecursive list compacting algorithm

    A nonrecursive list compacting algorithm Each Erlang process has its own stack and heap which are al ...

  9. ivy

    ivy https://ant.apache.org/ivy/ Apache Ivy™ is a popular dependency manager focusing on flexibility ...

  10. NettyIO