CF126B Password【KMP】By cellur925
其实$Chemist$在之前写了非常棒的题解!
我长话短说,补充两句。
- “那么当$next[n]$>$max$时显然不能将$next[n]$作为最长子串的长度”这句话其实在说,因为一个合法的串结尾肯定在2~$n$-1中,而$next[n]$的结尾在$n$处,显然是达不到的。那么我们就要继续缩小范围。直到存在合法串。
- $next[n]$等于0时无解?根据$next$数组的含义,我们知道它连相同前缀和后缀的匹配不上(没有),何谈中间部分的子串?
- 输出的时候找到合法的地点就结束程序,防止多输出东西。
$My$ $Code$
#include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; int len,j,maxx,tmp;
int nxt[];
char a[]; int main()
{
scanf("%s",a+);
len=strlen(a+);
for(int i=;i<=len;i++)
{
while(j&&a[i]!=a[j+]) j=nxt[j];
if(a[i]==a[j+]) j++;
nxt[i]=j;
if(i!=len) maxx=max(maxx,nxt[i]);
}
if(!nxt[len]){printf("Just a legend");return ;}
tmp=nxt[len];
while(tmp>maxx) tmp=nxt[tmp];
if(!tmp) {printf("Just a legend");return ;}
for(int i=;i<len;i++)
if(tmp==nxt[i])
{
for(int j=i-tmp+;j<=i;j++)
printf("%c",a[j]);
return ;
}
return ;
}
今天复习(学习)了下$KMP$算法,感觉这个算法最精妙的部分还是在$next$数组,而且体现了迭代的思想(?)“$j$=$next[j]$”,陌生的字符串问题可与$next$做类比,稍加改动进行求解。
另外欢迎大家资瓷文雀乐队新专辑《廟雨連珠》,链接藏在了这篇随笔的某个地方233.
CF126B Password【KMP】By cellur925的更多相关文章
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
- 【POJ2752】【KMP】Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- 【POJ2406】【KMP】Power Strings
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
随机推荐
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- Computer form factor
http://en.wikipedia.org/wiki/Motherboard_form_factor Computer form factor From Wikipedia, the free e ...
- hdu 4858 项目管理(vector模拟)
# include <stdio.h> # include <algorithm> # include <string.h> # include <vecto ...
- linux 输入子系统(3) button platform driver
button platform driver 一般位于driver/input/keyboard/gpio_keys.c /*用于按键事件的上报,它将在按键的中断发生后被调用.其中逻辑就是获取到按键类 ...
- 第一讲:使用html5——canvas绘制奥运五环
<html> <head> <title>初识canvas</title> </head> <body> <canvas ...
- 3144: [Hnoi2013]切糕
3144: [Hnoi2013]切糕 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1526 Solved: 827[Submit][Status] ...
- monitor and move the log content to our big data system
Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...
- 该项目不在c:\ 请确认该项目的位置
该项目不在c:\ 请确认该项目的位置 - CSDN博客https://blog.csdn.net/feilong1lantern/article/details/50388414 在删除不掉的文件夹目 ...
- ul li menu
第一步:建立一个无序列表我们先建立一个无序列表,来建立菜单的结构.代码是: <ul><li><a href="1">首页</a>&l ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...