codeforces 126B
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.
Example
fixprefixsuffix
fix
abcdabc
Just a legend
完全没有想法。先开始脑抽了,想了个二分,很激动的认为对了,结果wa,然后发现公共前缀的公共后缀的长度似乎不能二分。。。gg
又学了一下kmp。。。之前学的忘光了。。。
还是不太懂(mark)
这道题巧妙地利用了kmp中的nxt,这里的nxt就是kmp中预处理的失配函数。nxt[i]表示的是[1-i]位置的字符串中最长的前缀和后缀的公共部分的长度(s=abcab的nxt[5]=2,因为s[1-2]=s[4-5])
我们要求的正好和nxt所表示的东西很相似,于是就用上。
求完nxt,把每个长度标记。nxt[n]不标记,因为这是开头和结尾,不能算作中间的最长长度
从nxt[n](n是字符串长度)开始,表示的是整个串的前后缀最长公共部分的长度,如果这个长度存在,那么就可以了。(因为这个是最大的nxt,nxt是递减的)
挖个坑,似乎还是不是很理解
似乎理解了,就是公共长度不断缩小,但是kmp。。。
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000010
int n;
int nxt[N],used[N];
char s[N];
void Init()
{
int j=;
for(int i=;i<=n;i++)
{
while(j&&s[i]!=s[j+]) j=nxt[j];
if(s[i]==s[j+]) j++;
nxt[i]=j;
}
}
int main()
{
scanf("%s",s+);
n=strlen(s+);
Init();
for(int i=;i<n;i++) used[nxt[i]]=;
used[]=;
for(int i=n;i;i=nxt[i]) if(used[nxt[i]])
{
for(int j=;j<=nxt[i];j++) printf("%c",s[j]);
return ;
}
printf("Just a legend");
return ;
}
codeforces 126B的更多相关文章
- Codeforces 126B. Password(KMP,DP)
Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...
- CodeForces 126B Password
题目链接:http://codeforces.com/problemset/problem/126/B 题目大意: 多组数据每组给定1个字符串S,问是否存在S的一个尽量长的子串,同时是S的前缀和后缀, ...
- Codeforces 126B. Password (KMP)
<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...
- Codeforces 126B(kmp)
要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...
- 【Codeforces 126B】Password
[链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串 ...
- Codeforces 126B Password(Z算法)
题意 给定一个字符串 \(s\) ,求一个子串 \(t\) 满足 \(t\) 是 \(s\) 的前缀.后缀且在除前缀后缀之外的地方出现过. \(1 \leq |s| \leq 10^6\) 思路 \( ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- 在matlab中对中国地图中的不同省份按照高度进行渲染
直接上优化后的代码和效果图 colour_totall=128; % 载入地图数据 --各省的多边形数据 shp_data=shaperead('maps/bou2_4p.shp', 'UseGeoC ...
- jQuery.ajax()方法中參数具体解析
前言 在项目开发中,为了实现异步向服务端发起请求,最常常使用的就是jQuery.ajax方法了.刚開始需求比較简单,调用jQuery.ajax方法时要传的參数也就那几个常见的參数:url/data/d ...
- Mysql 之配置文件my.cnf
mysql配置文件为my.cnf,它所在位置根据安装时设定的. 当mysqld服务启动的时候,默认会按一定的顺序读取配置文件的. 1 2 3 [root@zhu2 ~]# /opt/mysql/lib ...
- CVE-2014-4114 和 CVE-2014-3566
这两天关注安全的人员都会特别留意这两个新披露的漏洞:CVE-2014-4114 和 CVE-2014-3566.以下我们就针对这两个漏洞最一些简要说明. CVE-2014-4114------- ...
- Java基础 笔记(四)
泛型: 泛型:通过<数据额类型>来接受一种引用数据类型.作用在编译时.使用检查 加入集合的对象是否属于该类型,从而把执行时的问题转换为编译时异常.提高程序的安全性 避免抛出 ClassCa ...
- HDU 1003 Max Sum (动规)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- subclassdlgitem
subclassdlgitem 该函数用来子类化一个控件. Subclass(子类化)是MFC中最常用的窗体技术之一.子类化完成两个工作:一是把窗体类对象attach到一个windows窗体实体中(即 ...
- IP数据报首部格式
IP协议提供不可靠.无连接的数据报传送服务. 不可靠:尽力而为地传输,不保证IP数据报能成功到达目的地. 无连接:每一个数据报之间相互独立地进行路由选择,可不按发送顺序接收. IP首部格式例如以下: ...
- ivy
ivy https://ant.apache.org/ivy/ Apache Ivy™ is a popular dependency manager focusing on flexibility ...
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow
原文链接:http://blog.chinaunix.net/uid-14607221-id-2794642.html 1. PreCreateWindow: Called by the framew ...