首页 技术 正文
技术 2022年11月6日
0 收藏 803 点赞 962 浏览 2041 个字

B. Passwordtime limit per test  2 secondsmemory 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 ;
}

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859