题目描述:

Password

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

Input

Copy

fixprefixsuffix

Output

Copy

fix

Input

Copy

abcdabc

Output

Copy

Just a legend

思路:

题目的意思是给一个字符串,看是否存在这样一个满足条件的最长字符子串:字串为前缀,后缀和中间的位置出现。刚开始时就把跟字符串的首字母相同的位置全部拿出来,因为只有这些位置可能会有满足条件的子串。然后从前往后,看从该位置出发到末尾的字串是否满足条件1是一种前缀2中间出现过。结果妥妥的在第52个测试点超时(一开始还以为能够过呢)。然后把string类全部改为c字符数组,写了字符串的比较函数,vector改用数组,加了读入优化,输出用puts,甚至吸了O2,还是超时。于是我动起了不好的念头,字符串的比较改用随机函数,只要每次随机的位置都相等,我就认为两个字符串相等,就不用每次线性的时间扫描了。嘿嘿,果然还是在第52个测试点T了。

此路不通,换条路。其实当题目提到前后缀时我就不自觉联想到了KMP算法,因为我隐约记得这是字符串的模式匹配(废话,当然是╭(╯^╰)╮),而且里面有个步骤就是跟前后缀有关的,还是线性时间复杂度,但是我已经不记得了(不,你根本就不知道(* ^ ▽^ *))。其实这道题就和KMP算法中的next表有关。next[i]表示的是模式串第i个位置之前的相同前后缀的最大长度,也是i个位置失效后要跳转的位置。那么我们再求出next后就可以根据它来判断最长相同前后缀是否在中间出现过,不是的话看次长相同前后缀在中间出现过没,再不是看次次长,...,直到跳到字符串外的-1

那么怎么判断是否在中间出现过呢?我们只需要在求出nxt表时同时用一个数组标记出nxt[i]的值是否出现。最后从末尾的末尾(len)位置开始,看nxt[i]值是否已被标记出现,出现就说明中间有这个长度的相同前后缀,没有标记就nxt[nxt[i]]地看次长位置中间标记没有,直到找到标记,记录长度后退出或者都不满足结束。

代码:

#include <iostream>
#include <string>
#define max_n 1000005
using namespace std;
string s;
int len;
int nxt[max_n];
int cnt[max_n];
int length = 0;
int get_nxt()
{
int j = 0;
int k = -1;
nxt[0] = -1;
while(j<len)
{
if(k==-1||s[k]==s[j])
{
++k;
++j;
nxt[j] = k;
}
else
{
k=nxt[k];
}
}
for(int i = 0;i<len;i++)
{
cnt[nxt[i]] = 1;
//cout << nxt[i] << " ";
}
//cout << endl;
}
int main()
{
cin >> s;
len = s.size();
get_nxt();
int k = nxt[len];
int flag = 0;
while(k>0)
{
if(cnt[k])
{
flag = 1;
length = k;
break;
}
k = nxt[k];
}
//cout << "k " << k << endl;
if(flag)
{
for(int i = len-length;i<len;i++)
{
cout << s[i];
}
cout << endl;
}
else
{
cout << "Just a legend" << endl;
}
return 0;
}

Codeforces A. Password(KMP的nxt跳转表)的更多相关文章

  1. Codeforces 126B. Password (KMP)

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

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

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

  3. oracle exp imp 导入 正在跳过表 plsql 导入表 成功终止 数据 被导入

    http://blog.csdn.net/agileclipse/article/details/12968011 .导入过程中,所有表导入都出现提示, 正在跳过表...某某表名 最后提示成功终止导入 ...

  4. 词典(一) 跳转表(Skip table)

    词典,顾名思义,就是通过关键码来查询的结构.二叉搜索树也可以作为词典,不过各种BST,如AVL树.B-树.红黑树.伸展树,结构和操作比较复杂,而且理论上插入和删除都需要O(logn)的复杂度. 在词典 ...

  5. CodeForces 126B Password

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

  6. Ubuntu输入password登陆后又跳回到登录界面

    现象:在Ubuntu登陆界面输入password之后.黑屏一闪而且出现了check battery state之类的文字之后,又跳转到登录界面. 原因:主文件夹下的.Xauthority文件拥有者变成 ...

  7. CF #93 div1 B. Password KMP/Z

    题目链接:http://codeforces.com/problemset/problem/126/B 大意:给一个字符串,问最长的既是前缀又是后缀又是中缀(这里指在内部出现)的子串. 我自己的做法是 ...

  8. [CF30E]Tricky and Clever Password(KMP+manacher)

    首先枚举回文中心,然后显然中心两边要尽量扩展作为middle,这个用manacher实现. 然后注意到suffix的结尾位置是固定的(串尾),那么预处理出以每个位置结尾的串与原串后缀至多能匹配多长,然 ...

  9. Codeforces126B - Password(KMP)

    题目大意 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 KMP的失配函数f[i]的非零值就是前i个字符的一个最长前缀且也是后缀的字符 ...

随机推荐

  1. quick 中 "我的项目" 中的列表从那里来的?

    quick 中 "我的项目" 中的列表从那里来的? 1. WelcomeScene.lua 场景 self:createOpenRecents(cc.player.settings ...

  2. CDN惹的祸:记一次使用OSS设置跨域资源共享(CORS)不生效的问题

    原文: https://www.lastupdate.net/4669.html 昨天H5组的开发反馈了一个问题,说浏览器收不到跨域的配置,提示:Failed to load https://nnmj ...

  3. 常用STL使用指北

    常用STL使用指北 set和multiset set和multiset都是基于红黑树(显然是一个二叉搜索树)的STL. 定义 我们可以使用(multi)set<元素类型>名称来定义一个(m ...

  4. Java集合详解7:一文搞清楚HashSet,TreeSet与LinkedHashSet的异同

    <Java集合详解系列>是我在完成夯实Java基础篇的系列博客后准备开始写的新系列. 这些文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查 ...

  5. 微信小程序云开发不完全指北

    微信小程序云开发不完全指北 首先必须说明云开发的"云"并不是类似云玩家里的云的意思,而是微信小程序真的提供了云开发的接口以及一个简单的提供存储.数据库服务的虚拟后台(对于一些轻量小 ...

  6. 【技术博客】使用PhpStorm和Xdebug实现Laravel工程的远程开发及调试

    目录 使用PhpStorm和Xdebug实现Laravel工程的远程开发及调试 简介 PhpStorm中的远程开发 1. 配置服务器 2. 配置路径对应 3. 配置同步 4. 进行代码同步 5. 优点 ...

  7. docker 镜像加速,修改为阿里云镜像

    首先访问 登录阿里云 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors,会获取专属的镜像地址 centos用户执行下列操作即可 s ...

  8. 【IntelliJ IDEA学习之三】IntelliJ IDEA常用快捷键

    版本:IntelliJIDEA2018.1.4 按场景列举一.打开设置CTRL + ALT + S:打开设置(File-->Settings...)Ctrl + Shift + Alt + S: ...

  9. Docker底层基石namespace与cgroup

    Docker底层基石namespace与cgroup   容器本质上是把系统中为同一个业务目标服务的相关进程合成一组,放在一个叫做namespace的空间中,同一个namespace中的进程能够互相通 ...

  10. Spring Security 解析(四) ——短信登录开发

    Spring Security 解析(四) -- 短信登录开发   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security ...