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. 洛谷P5156 [USACO18DEC]Sort It Out

    这题就是让你求字典序第k小的最短乱序子序列 转换一下,其实就是字典序第k大的最长上升子序列 就统计一下以i结尾的最长上升子序列\(f[i]\),长度为i的上升子序列的开头组成的集合\(v[i]\),转 ...

  2. Locust HTTP client

    Http client 默认是以安全模式运行的,任何由于连接错误.超时或者类似错误引起的异常,都会返回一个空的Response对象,这个请求将会再locust统计中标记为failure,返回的虚拟对象 ...

  3. springMVC中一些功能

    1.controller的生命周期 spring框架默认为单例模式,会使数据之间的传递互相影响,而springMVC给我们提供了request与session两个,request每次请求就会产生一个单 ...

  4. 【云计算】impala建表,文件关联,查询

    [cloudil@hadoop164 caixianfeng]$ hdfs dfs -mkdir -p /csv-pig/tab1 /csv-pig/tab1 tab1.csv: 1,true,123 ...

  5. RocketMQ 安装

    RocketMQ 安装 1.进入目录 cd /usr 2.下载 wget http://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.3.0/rocke ...

  6. Oracle 通过数据字典查询系统信息

    简介:数据字典记录了数据库系统的信息,他是只读表和视图的集合,数据字典的所有者是sys用户.注:用户只能在数据字典上执行查询操作,而维护和修改是由系统自己完成的. 1.数据字典的组成:数据字典包括数据 ...

  7. 008-PageBean类模板

    1 PageBean模板一 package ${enclosing_package}; import java.util.ArrayList; import java.util.List; publi ...

  8. 【ExtJS】contentEl的使用

    contentEl 指定一个已存在的HTML元素, 或者一个已存在HTML元素的 id , 它们将被用作当前组件的内容. 此配置选项被用来将一个已存在的HTML元素 插入到一个新组件(在组件渲染之后它 ...

  9. jquery清空下拉框,保留第一个

    js中可以document.getElementById("id").options.length = 1;设置 jquery中的设置方法:$("#id option[i ...

  10. 80端口被系统占用,无法kill

    哎,最近郁闷了,一直想用80端口配到APache上,可是老是找不到原因,Nginx 都停掉了,也没有装IIS,也没发出别的程序占用着80端口,又不想换到别的端口 一定要找到问题,坚持!!! 用cmd窗 ...