题干:

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

fixprefixsuffix

Output

fix

Input

abcdabc

Output

Just a legend

题目大意:

从一个串s中找出一个最长的子串t,该字串满足:

t是s的前缀,t是s的后缀,t是s的中缀,(定义t是s的中缀:t串在s串中出现,且既不是前缀也不是后缀)

(|s|<=1e6)

解题报告:

这题正解好像是exkmp。。但是也比较巧妙的是字符串Hash的做法。

首先我们知道,如果题目让求的是前缀和中缀,那直接二分+Hash即可。现在加上了后缀,貌似就不能直接二分+Hash了,因为当对于一个长度k满足,不一定对于k'也满足(其中k'<k),比如abcdabcdabcd,abcd是答案,但是ab就不是答案,因为ab不是后缀。对于这种情况的一个处理就是:先On预处理出来所有的可行前缀后缀,然后再预处理的数组中去二分。也就是先用后缀来约束一部分前缀,使得目前可行前缀都是满足前缀==后缀的,所以只需要判断前缀和中缀的关系就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
typedef unsigned ll ull;
const int MAX = 1e6 + 5;
const ull seed = 131;
ull Hash[MAX],P[MAX];
char s[MAX];
int n;
ull get(int l,int r) {return Hash[r]-Hash[l-1]*P[r-l+1];
}
int b[MAX],tot;
bool ok(int x) {ull tar = get(1,x);for(int l = 2; l+x-1<n; l++) {int r = l+x-1;if(tar == get(l,r)) return 1;}return 0;
}
int main()
{cin>>s+1;n = strlen(s+1);P[0]=1;for(int i = 1; i<=n; i++) P[i] = P[i-1]*seed;for(int i = 1; i<=n; i++) {Hash[i] = Hash[i-1]*seed + s[i]-'a'+1;}for(int i = 1; i<=n; i++) {if(get(1,i) == get(n-i+1,n)) b[++tot] = i;}int l=1,r=tot,ans=-1,mid;while(l<=r) {mid = (l+r)>>1;if(ok(b[mid])) l = mid+1,ans = b[mid];else r = mid-1;}if(ans == -1) {printf("Just a legend\n");return 0;}for(int i = 1; i<=ans; i++) printf("%c",s[i]);return 0 ;
}

【Codeforces - 127D】Password(思维,二分+字符串Hash)相关推荐

  1. 【题解】 Codeforces Edu41 F. k-substrings (字符串Hash)

    题面戳我 Solution 我们正着每次都要枚举从长到短,时间复杂度承受不了,但是我们可以发现一个规律,假设某次的答案为\(x\),那么这个字符串为\(A+X+B\)组成,无论中间的\(X\)是重叠还 ...

  2. Voltage Keepsake CodeForces - 801C (思维+二分)

    题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...

  3. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  4. CodeForces - 727E Games on a CD 字符串Hash

    题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个 ...

  5. Wannafly挑战赛11 A B D【规律+逆元+字符串hash】

    链接:https://www.nowcoder.com/acm/contest/73/A 来源:牛客网 题目描述 白兔学会了分身术. 一开始有一只白兔,接下来会进行k轮操作,每一轮中每一只白兔都会变成 ...

  6. hdu4821 字符串hash

    参考博客:点击打开链接 字符串hash典例. 这里用的是bkdrhash 法.也是最常用的冲突最少的一种.原理:把字符串和数值对应.这里用base=31(一般用质数), 先是扫一遍,处理处每个位子到结 ...

  7. hdu4821 字符串hash(有多少(M*L长的,M个不相同)子串))

    题意这英语..反正我是读不懂== 题意:给定一个串,有多少M*L的子串,其中子串的M个子串不相同= (注:某一位不相同即为不相同) 很明显要On的扫,但是似乎前一个与后一个没有关系?是的,前第x个(x ...

  8. 字符串Hash的原理与应用

    字符串Hash无论是在ACM竞赛中还是在工程中都有着广泛的应用,所以很有必要掌握好它的用法.主要分为两个部 分:Hash映射和冲突处理.而本文主要来详细讲解Hash映射的方法及应用,下篇文章将会介绍如 ...

  9. 字符串hash(二)

    从上一届已经讲了字符串hash的方法,hash后怎么用也很重要 文章目录 一.查询子串的hash值 查询子串减去期中一个字符后的hash值 查询两个子串拼接的hash值 **hash的模板(自然溢出) ...

最新文章

  1. php隐藏webshell_【web端权限维持】利用ADS隐藏webshell
  2. 制作一个表格,显示班级的学生信息。
  3. 响应式网站设计_通过这个免费的四小时课程,掌握响应式网站设计
  4. android底层设置相机帧率,Android Camera previewFrame 提高 fps
  5. Observable观察者模式的使用
  6. Win32下显示、隐式加载DLL的方法
  7. connection url mysql,JDBC 连接MySQL实例详解
  8. Java设计模式学习总结(2)——创建型模式之工厂模式
  9. IOS开发-TableView表视图LV2
  10. 软件工程中需要学习和掌握的软件都有哪些_高三孩子:你想学软件工程,要先知道这些...
  11. oracle getpy,拼音
  12. SpringBoot——springboot SPI原理与实战
  13. S7-200SMART与昆仑通态触摸屏以太网通信的具体方法和步骤(图文)
  14. SpringBoot集成JApiDocs实现自动生成接口文档
  15. win10插上耳机还外放解决解决方法
  16. 7、前后端分离中的权限管理思路
  17. i7 10510u相当于什么处理器
  18. 网站中qq 跳转 和qq群问题
  19. Excel 画函数曲线
  20. Qt实现基于G.729A(G729A)的语音聊天

热门文章

  1. 出口同比中国经济三大怪状折射出啥危机?
  2. [SAP FI] Bank Master Vendor Master Creation Related Knowledge
  3. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第33篇]Bellcore攻击是如何攻击使用CRT的RSA的?
  4. linux 5识别网卡,CentOS 5.5系统识别不了Atheros AR8151网卡怎么办?
  5. 476B. Dreamoon and WiFi
  6. 计算机应用基础试模块5ACCSE,2015年计算机二级《Access》上机最后冲刺卷(1)
  7. i = i++ 之后 i 的值为什么是 0
  8. oracle数据库领域,菜鸟成长课程之《Oracle数据库职业直通车》,引领大家真正进入Oracle数据库领域...
  9. NSIS 查找文件是否存在,并设置安装路径
  10. 非阻IO与EWOULDBLOCK EAGAIN