洛谷P3435 [POI2006]OKR-Period of Words [KMP]
OKR-Period of Words
Description
Input
Output
Sample Input
babababa
Sample Output
分析:
又是一道需要深入理解$next[]$数组的题。
因为$next[i]$表示的是第$i-1$个前缀的最长公共前后缀,那么不难想到,在$j<=i$且$next[j]$不为$0$的情况下可以得到一个最短后缀,用原串长度减去即可得到答案。
Code:
//It is made by HolseLee on 11th Aug 2018
//Luogu.org P3435
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std; const int N=2e6+;
int n,nxt[N],k,cnt;
long long ans;
char s[N]; int main()
{
scanf("%d%s",&n,s);
nxt[]=nxt[]=;k=;
for(int i=;i<n;++i){
while(k&&s[i]!=s[k])
k=nxt[k];
nxt[i+]=(s[i]==s[k]?++k:);
}
int ka;
for(int i=;i<=n;++i){
ka=i;
while(nxt[ka])ka=nxt[ka];
if(nxt[i])nxt[i]=ka;
ans+=(i-ka);
}
printf("%lld ",ans);
return ;
}
洛谷P3435 [POI2006]OKR-Period of Words [KMP]的更多相关文章
- 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)
洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...
- 洛谷 P3435 [POI2006]OKR-Periods of Words
题目传送门 解题思路: 这道题题面比较乱,先说一下这道题要求什么: 对于一个字符串,求它及它的所有前缀的一个答案串的长度之和,答案串就是对于一个字符串,找到一个它的一个前缀,这个前缀后面在复制一遍,得 ...
- 2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP)
2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP) https://www.luogu.com.cn/problem/P3435 题意: 对于一个仅 ...
- [洛谷P3444] [POI2006]ORK-Ploughing
洛谷题目链接[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He ca ...
- 洛谷P3434 [POI2006]KRA-The Disks(线段树)
洛谷题目传送门 \(O(n)\)的正解算法对我这个小蒟蒻真的还有点思维难度.洛谷题解里都讲得很好. 考试的时候一看到300000就直接去想各种带log的做法了,反正不怕T...... 我永远只会有最直 ...
- 洛谷P3434 [POI2006]KRA-The Disks [模拟]
题目传送门 KRA 题目描述 For his birthday present little Johnny has received from his parents a new plaything ...
- 洛谷 P3437 [POI2006]TET-Tetris 3D 解题报告
P3437 [POI2006]TET-Tetris 3D 题目描述 The authors of the game "Tetris" have decided to make a ...
- 洛谷P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
- 洛谷 P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
随机推荐
- ios 虚拟机中文件下载路径
每个人mac上的路径会有不同,你可以打印出你文件下载存放的路径,然后拷贝一下,再单击桌面空白处,最上面的导航栏上有个“前往”,然后找到“前往文件夹”,粘贴一下,就可以找到了.
- 4.redis设计与实现--跳跃表
1.跳跃表由两个结构体构成: 2.总结:
- 用英文写Email的注意事项
- ② 设计模式的艺术-08.桥接(Bridge)模式
为什么需要桥接(Bridge)模式 商城系统中常见的商品分类,以电脑为类,如何良好的处理商品分类销售的问题? 采用多层继承结构: 多层继承结构代码示例 Computer.java package co ...
- 【BZOJ】1297: [SCOI2009]迷路
[题意]给定n个点的有向带边权图,求0到n-1长度恰好为T的路径数.n<=10,T<=10^9,边权1<=wi<=9. [算法]矩阵快速幂 [题解]这道题的边权全部为1时,有简 ...
- Java的继承和多态
看了博客园里面的一个文章,关于java的继承和多态: class A ...{ public String show(D obj)...{ return ("A and D"); ...
- python简单爬虫(二)
上一篇简单的实现了获取url返回的内容,在这一篇就要第返回的内容进行提取,并将结果保存到html中. 一 . 需求: 抓取主页面:百度百科Python词条 https://baike.baidu. ...
- PHP对象4: final 不允许重写方法或不允许继承类
final用在方法中,能继承方法, 不允许重写方法 final用在类声名中, 此类就不能继承 <?php class A{ final function say(){ say 'Ok<br ...
- 全局应用程序类(Global.asax)
注:该部分参考的园区的“积少成多”的 <ASP.NET MVC中的Global.asax文件> . 1.Global.asax文件介绍 global.asax这个文件包含全局应用程序事件 ...
- What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)
I go to the gym and lift weights because I like the feeling of getting stronger and better. Two mont ...