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

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439696.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

百度顶会论文复现(2):GAN综述

本节课主要是对GAN的发展进行了介绍&#xff0c;包括基本原理&#xff0c;训练方法&#xff0c;存在问题&#xff0c;改进以及应用场景等。实践作业则为手写数字生成。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/preview/493290。 文章目录1.什么是…

一步步编写操作系统 48 二进制程序的加载方式

接上节&#xff0c;程序头可以自定义&#xff0c;只要我们按照自己定义的格式去解析就行。也许我光这么一说&#xff0c;很多同学还是不能彻底明白如何自定义文件头&#xff0c;因为大多数同学都是用高级语言来写程序&#xff0c;即使用了偏底层的c语言&#xff0c;不同平台的c…

【Codeforces - 864D】Make a Permutation!(贪心,字典序)

题干&#xff1a; Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his arra…

百度顶会论文复现(3):视频分类综述

本节课主要是对视频分类的发展进行了介绍&#xff0c;包括任务与背景&#xff0c;分类方法&#xff0c;前沿进展等。课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/course/introduce/1340?directly1&shared1。 文章目录1. 任务与背景2. 视频分类方法2.1 双流…

一步步编写操作系统 46 linux的elf可执行文件格式1

ELF文件格式依然是分为文件头和文件体两部分&#xff0c;只是该文件头相对稍显复杂&#xff0c;类似层次化结构&#xff0c;先用个ELF header从“全局上”给出程序文件的组织结构&#xff0c;概要出程序中其它头表的位置大小等信息&#xff0c;如程序头表的大小及位置、节头表的…

百度顶会论文复现(4):飞桨API详解

本节课主要是对飞桨常用API进行了介绍&#xff0c;课程地址为&#xff1a;https://aistudio.baidu.com/aistudio/education/group/info/1340。 文章目录1.飞桨API官网2. API使用介绍3. 飞桨模型操作1.飞桨API官网 官网地址为&#xff1a;https://www.paddlepaddle.org.cn/docu…

【Codeforces - 977D】Divide by three, multiply by two(思维构造)

题干&#xff1a; Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1 operations of the two kinds: divide the number xx by 33 (xx must be divisible by 33);multiply the numbe…

一步步编写操作系统 45 linux的elf可执行文件中的段和节

接上文&#xff0c;为了描述清楚文件格式的本质&#xff0c;咱们先从最基本的“段”说起。 程序中最重要的部分就是段&#xff08;segment&#xff09;和节&#xff08;section&#xff09;&#xff0c;它们是真正的程序体&#xff0c;是真真切切的程序资源&#xff0c;所以下…

视觉SLAM十四讲(3):三维空间刚体运动

本章需要掌握的知识点有&#xff1a;旋转矩阵&#xff0c;变换矩阵&#xff0c;四元数&#xff0c;欧拉角定义和数学表达&#xff1b;同时也要掌握Eigen库关于矩阵、几何模块的使用方法。 文章目录3.1 旋转矩阵3.1.1 点&#xff0c;向量和矩阵的关系3.1.2 坐标系间的欧式变换3.…

【CodeForces - 483C】Diverse Permutation(思维构造)

题干&#xff1a; Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct positive integers not larger than n. Well denote as n the length of permutation p1,   p2,   ...,   pn. Your task is to find such…

一步步编写操作系统 47 elf格式文件分析实验

在上一节中&#xff0c;我们讲述了elf格式的部分理论知识&#xff0c;为什么是部分呢&#xff1f;因为我们本着“够用”的原则&#xff0c;只把我们需要了解的部分说完啦。不过&#xff0c;我相信大部分同学仅仅凭上一节中的理论知识还是领悟不到elf本质&#xff0c;咱们在本节…

百度飞桨顶会论文复现(5):视频分类论文之《Representation Flow for Action Recognition》篇

这次老师在课上总共领读了4篇分类论文&#xff0c;我这里分享其中的一篇论文&#xff0c;是关于使用神经网络对光流进行学习。 课程地址是&#xff1a;https://aistudio.baidu.com/aistudio/education/group/info/1340。 论文地址是&#xff1a;https://arxiv.org/abs/1810.014…

智能算法(GA、DBO等)求解零等待流水车间调度问题(NWFSP)

先做一个声明&#xff1a;文章是由我的个人公众号中的推送直接复制粘贴而来&#xff0c;因此对智能优化算法感兴趣的朋友&#xff0c;可关注我的个人公众号&#xff1a;启发式算法讨论。我会不定期在公众号里分享不同的智能优化算法&#xff0c;经典的&#xff0c;或者是近几年…

【蓝桥官网试题 - 算法提高】change(思维)

题干&#xff1a; 问题描述 数组A中共有n个元素&#xff0c;初始全为0。你可以对数组进行两种操作&#xff1a;1、将数组中的一个元素加1&#xff1b;2、将数组中所有元素乘2。求将数组A从初始状态变为目标状态B所需要的最少操作数。 输入格式 第一行一个正整数n表示数组中元…

一步步编写操作系统 50 加载内核3

接上节&#xff0c;在这里&#xff0c;我们把参数放到了栈中保存&#xff0c;大家注意到了&#xff0c;参数入栈的顺序是先从最右边的开始&#xff0c;最后压入的参数最左边的&#xff0c;其实这是某种约定&#xff0c;要不&#xff0c;为什么不先把中间的参数src入栈呢。既然主…

【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维)

题干&#xff1a; The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator. There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrige…

动手学无人驾驶(5):多传感器数据融合

本系列的前4篇文章主要介绍了深度学习技术在无人驾驶环境感知中的应用&#xff0c;包括交通标志识别&#xff0c;图像与点云3D目标检测。关于环境感知部分的介绍本系列暂且告一段落&#xff0c;后续如有需要再进行补充。 现在我们开启新的篇章&#xff0c;在本文中将会介绍无人…

一步步编写操作系统 52 深入浅出cpu的特权级

所谓保护模式下的“保护”&#xff0c;主要体现在特权级上&#xff0c;以后随着后面工作的展开&#xff0c;会越来越多的和它们打交道&#xff0c;现在是时候说道说道了。 在人类社会中出现恶势力时&#xff0c;人们总是希望出现一位具有神力的英雄来拯救世人、主持公道。阶级…

详解两阶段3D目标检测网络PVRCNN:Point-Voxel Feature Set Abstraction for 3D Object Detection

在《动手学无人驾驶&#xff08;4&#xff09;&#xff1a;基于激光雷达点云数据3D目标检测》一文中介绍了3D目标检测网络PointRCNN。今天介绍该作者新提出的3D检测模型&#xff1a;PVRCNN&#xff0c;论文已收录于CVPR2020。 作者个人主页为&#xff1a;https://sshaoshuai.gi…

【Codeforces - 755C】PolandBall and Forest(并查集)

题干&#xff1a; PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree. There is exactly one …