codeforces 126B

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是递减的)挖个坑,似乎还是不是很理解
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000010
int n;
int nxt[N],used[N];
char s[N];
void Init()
{
    int j=0;
    for(int i=2;i<=n;i++)
    {
        while(j&&s[i]!=s[j+1]) j=nxt[j];
        if(s[i]==s[j+1]) j++;
        nxt[i]=j;
    }
}
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    Init();
    for(int i=1;i<n;i++) used[nxt[i]]=1;
    used[0]=0;
    for(int i=n;i;i=nxt[i]) if(used[nxt[i]])
    {
        for(int j=1;j<=nxt[i];j++) printf("%c",s[j]);
        return 0;
    }
    printf("Just a legend");
    return 0;
}
时间: 2024-12-28 21:54:44

codeforces 126B的相关文章

codeforces 126B Password

题意:给你一个字符串 ,问你既是它的前缀 ,又是它的后缀,且是在中间出线过的最长字串是什么 解题思路:KMP变形,不熟悉next写出来还是有点困难 解题代码: 1 // File Name: 126b.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月07日 星期六 15时23分51秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include

【Codeforces 126B】Password

[链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串t [题解] KMP的应用 f[i]就是以i为结尾的后缀能匹配的最长前缀的长度 因此只要知道f[n]的值. 然后在做kmp的时候,看看中间有没有哪个时刻能匹配到长度为f[n]的前缀就好(开个数组标记一下就好); 如果没有就让j = f[f[n]] 直到匹配不到为止. [代码] import java

Codeforces 126B. Password (KMP)

<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数组的性质.具体做法见代码. #include <bits/stdc++.h> using namespace std; const int N = 1e6+5; char str[N]; int vis[N],nxt[N]; void getNext(int len){ int j=0,k=-1;

Codeforces 126B(kmp)

要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cstdio> #include <cstring> const int maxn = 1e6 + 5; char s[maxn]; int Next[maxn], Has[maxn], flag; int main() { scanf("%s", s + 1); int n

Password Uva1262 KMP

D - Password Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 126B Description Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However,

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st