time limit per test  2 seconds
memory limit per test  256 megabytes
 

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.

input
fixprefixsuffix
output
fix

input
abcdabc
output
Just a legend

题目大意:

     给你一个字符串,让你在里面找到一个最长的公共的前缀后缀,并且在 字符串中间也出现过一次的子串。

解题思路:

     这个题KMP的next数组的理解还是要有的,next[i]表示在i之前,最长的 公共前缀后缀的长度。

     所以说,我们首先要看看是否存在公共前缀后缀, 如果有,这只是保证了可能有解,因为我们还要看中间是否出现过,

     这个时候,我们让i=next[i],继续看这个next[i]是否出现过,为什么呢? 因为你此往前移动是,就相当于产生了一个可能的答案,

     但是我们需要中间 也出现过,所以就要判断这个next[i]值是否出现过,当出现过的就是答案。

 #include <stdio.h>
#include <string.h> char s[];
int next_[],vis[]; void getnext() // 得到next数组
{
int i = , j = -;
int len = strlen(s);
next_[] = -;
while (i < len){
if (j == - || s[i] == s[j])
next_[++ i] = ++ j;
else
j = next_[j];
}
} int main ()
{
int i;
while (~scanf("%s",s)){
int len = strlen(s);
getnext();
memset(vis,,sizeof(vis));
for (i = ; i < len; i ++) //将各个位置的最长前后缀标记
vis[next_[i]] = ; int j = len,flag = ;
while (next_[j]>){
if (vis[next_[j]]){
for (i = ; i < next_[j]; i ++)
printf("%c",s[i]);
printf("\n");
flag = ; break;
}
j = next_[j];
}
if (!flag)
printf("Just a legend\n");
}
return ;
}

Codeforces(Round #93) 126 B. Password的更多相关文章

  1. Educational Codeforces Round 93 (Rated for Div. 2)题解

    A. Bad Triangle 题目:https://codeforces.com/contest/1398/problem/A 题解:一道计算几何题,只要观察数组的第1,2,n个,判断他们能否构成三 ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #370 - #379 (Div. 2)

    题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi  ...

随机推荐

  1. 04. H5标签有哪些?行内元素有哪些?块级元素有哪些?空(void)元素有哪些?行内元素和块级元素有什么区别?你工作中常用标签有什么?

    4. H5标签有哪些? 2)行内元素有哪些? a - 锚点 em - 强调 img - 图片 font - 字体设定 ( 不推荐 ) i - 斜体 input - 输入框 3)块级元素有哪些? add ...

  2. DOM操作 045

    一 什么是DOM DOM : 文档对象模型 它为文档提供了结构化表示 并定义了如何通过脚本来访问文档结构 . 目的就是为了能让js操作HTML元素而制定的一个规范 . DOM树(一切都是节点): 元素 ...

  3. python 库 、包 、模块

    概念: 模块: 模块是一种以.py为后缀的文件,在.py文件中定义了一些常量和函数.模块的名称是该.py文件的名称.模块的名称作为一个全局变量__name__的取值可以被其他模块获取或导入. 模块的导 ...

  4. docker 把容器commit成镜像

    该方法是使用docker commit 命令,其命令格式为:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] 主要参数选项包括: -a ,– ...

  5. gdb调试工具常用命令

    编译程序时需要加上-g,之后才能用gdb进行调试:gcc -g main.c -o main gdb中命令: 回车键:重复上一命令 (gdb)help:查看命令帮助,具体命令查询在gdb中输入help ...

  6. css3毛玻璃效果白边问题

    注:css3毛玻璃效果应该很多人都知道怎么实现,但是有个问题是图片模糊了之后相当于缩小了,所以颜色深的图片会出现白边,这里说下我参考网上的解决方式吧! 1.毛玻璃实现方法: CSS3 blur滤镜实现 ...

  7. php的stristr()函数,查找字符

    1.用法,要传2个参数 stristr(string,search):查找并返还匹配后,剩下的部分字符串 查找过程不区分大小写,要区分大小写用 strstr(string,search)少一个字母i ...

  8. js emoji表情长度判断

    1.需求:输入框长度限制为10个字符,包括表情.超出长度提示. 注:iPhone手机自定义的表情,有四个小人的,三个小人的,主要是长度还都不一样.有的表情可能一个就超出了长度限制(10),比如

  9. TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.Internal.IHttpResponseStreamWriterFactory' from assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.2.0 ...

    今天调试 asp.net core 2.0 项目时遇到了如下错误: TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.I ...

  10. git提交空文件夹和删除远程文件

    git提交空文件夹 在文件夹中创建 .gitkeep 文件,文件内容如下 # Ignore everything in this directory * # Except this file !.gi ...