洛谷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 ...
随机推荐
- 远程调试openstack
之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰: http://blog.csdn.net/tantexian/article/ ...
- HTML5笔记-加强版
新增的语法结构表单验证 1.新的页面结构以及宽松的语法规范:<!doctype html> <meta charset=“utf-8”/> 2.新的结构化元素:语义化标签: ...
- HDU 1431 思维 基础数论
找范围内回文素数,最大到1e8,我就是要枚举回文串,再判素数,然后因为这种弱智思路死磕了很久题目. /** @Date : 2017-09-08 15:24:43 * @FileName: HDU 1 ...
- (转)使用Excel批量给数据添加单引号和逗号
在使用PLSQL连接oracle数据库处理数据的过程中,常用的操作是通过ID查询出数据,ID需要附上单引号,如果查询的ID为一条或者几条,我们手动添加即可,但是如果是几百条.几千条的话,就需要使用一些 ...
- 2D旋转和3D旋转
2D旋转 先给个容器 <p onClick="rotate2D()" id="rotate2D" class="animated_div&quo ...
- cropper.js 跨域问题
this.$clone = $clone = $('<img>'); $clone.one('load', $.proxy(function () { var naturalWidth = ...
- macbook 安装任意来源
sudo spctl --master-disable Comand+r Csrutil disable Reboot
- css3背景色过渡
<!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...
- CF148A Insomnia cure
公主睡前数龙, 每隔k, l, m, n只都会用不同的技能攻击龙. 假定共数了d只龙, 问共有多少龙被攻击了. 思路: 用一个visit数组记录被攻击过的dragon, 最后遍历visit数组统计被攻 ...
- 项目开发 -- ZFS容量分配
存储池 allocated 池中已实际分配的存储空间量.该属性也可通过其简短列名alloc来引用. capacity 已用的池空间百分比.此属性也可通过其简短列名cap来引用. dedupratio ...