题目描述:

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. .NETCore_项目启动设置域名以及端口

    //第一种方式就是启动是一个命令窗口 public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.C ...

  2. html页面添加左侧滑动菜单与内容部分的滚动条

    html + css + jquery 展示地址:https://migloo.gitee.io/front  或 https://www.igloo.xin/front 思路: 1.通过jquery ...

  3. Kubernetes集群之清除集群

    清除K8s集群的Etcd集群 操作服务器为:192.168.1.175/192.168.1.176/192.168.1.177,即etcd集群的三台服务器.以下以192.168.1.175为例子. 暂 ...

  4. 关于 Keras 模型

    在 Keras 中有两类主要的模型:Sequential 顺序模型 和 使用函数式 API 的 Model 类模型. 1.Sequential 顺序模型 ====>>开始使用 Keras ...

  5. Python【每日一问】25

    问: [基础题]:利用条件运算符的嵌套来完成此题:学习成绩 >=90 分的同学用 A 表示,60-89 分之间的用 B 表示,60 分以下的用 C 表示. [提高题]:求 s=a + aa + ...

  6. [转帖]【mount】Linux根目录空间不足

    [mount]Linux根目录空间不足 2019.04.15 21:30:47字数 1094阅读 107 一.问题背景 一台数据库服务器,突然监控告警,报根目录空间不足(no space left o ...

  7. DEV 总结

    转自:https://www.cnblogs.com/yuerdongni/archive/2012/09/08/2676753.html 1. 如何解决单击记录整行选中的问题 View->Op ...

  8. 再谈初学者关心的ssh应用方方面面

    http://blog.robertelder.org/what-is-ssh/ https://www.ssh.com/ssh/key/ 什么是ssh? ssh是一个在计算机之间实现安全通信的网络协 ...

  9. javascript异步编程学习及实例

    所谓异步就是指给定了一串函数调用a,b,c,d,各个函数的执行完结返回过程并不顺序为a->b->c->d(这属于传统的同步编程模式),a,b,c,d调用返回的时机并不确定. 对于js ...

  10. Spring Security实现OAuth2.0授权服务 - 基础版

    一.OAuth2.0协议 1.OAuth2.0概述 OAuth2.0是一个关于授权的开放网络协议. 该协议在第三方应用与服务提供平台之间设置了一个授权层.第三方应用需要服务资源时,并不是直接使用用户帐 ...