洛谷传送门BZOJ传送门

OKR-Period of Words

Description

一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, 使得 A = PB. 如果 P A 并且 P 不是一个空串,那么我们说 P 是A的一个proper前缀. 定义Q 是A的周期, 当且仅当Q是A的一个proper 前缀并且A是QQ的前缀(不一定要是proper前缀). 比如串 abab 和 ababab 都是串abababa的周期. 串A的最大周期就是它最长的一个周期或者是一个空串(当A没有周期的时候), 比如说, ababab的最大周期是abab. 串abc的最大周期是空串. 给出一个串,求出它所有前缀的最大周期长度之和.

Input

第一行一个整数 k ( 1 k 1 000 000) 表示串的长度. 接下来一行表示给出的串.

Output

输出一个整数表示它所有前缀的最大周期长度之和.

Sample Input

8
babababa

Sample Output

24

  分析:

  又是一道需要深入理解$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]的更多相关文章

  1. 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)

    洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...

  2. 洛谷 P3435 [POI2006]OKR-Periods of Words

    题目传送门 解题思路: 这道题题面比较乱,先说一下这道题要求什么: 对于一个字符串,求它及它的所有前缀的一个答案串的长度之和,答案串就是对于一个字符串,找到一个它的一个前缀,这个前缀后面在复制一遍,得 ...

  3. 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 题意: 对于一个仅 ...

  4. [洛谷P3444] [POI2006]ORK-Ploughing

    洛谷题目链接[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He ca ...

  5. 洛谷P3434 [POI2006]KRA-The Disks(线段树)

    洛谷题目传送门 \(O(n)\)的正解算法对我这个小蒟蒻真的还有点思维难度.洛谷题解里都讲得很好. 考试的时候一看到300000就直接去想各种带log的做法了,反正不怕T...... 我永远只会有最直 ...

  6. 洛谷P3434 [POI2006]KRA-The Disks [模拟]

    题目传送门 KRA 题目描述 For his birthday present little Johnny has received from his parents a new plaything ...

  7. 洛谷 P3437 [POI2006]TET-Tetris 3D 解题报告

    P3437 [POI2006]TET-Tetris 3D 题目描述 The authors of the game "Tetris" have decided to make a ...

  8. 洛谷P3434 [POI2006]KRA-The Disks

    P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...

  9. 洛谷 P3434 [POI2006]KRA-The Disks

    P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...

随机推荐

  1. 在vue中使用animate.css

    animate.css是一款前端动画库,相似的有velocity-animate 用法: 首先 npm install animate.css --save 然后在vue文件的script中引入: i ...

  2. swift开发常用代码片段

    // 绑定事件 cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switch ...

  3. Java集合(4)一 红黑树、TreeMap与TreeSet(下)

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

  4. 【CodeForces】790 C. Bear and Company 动态规划

    [题目]C. Bear and Company [题意]给定大写字母字符串,交换相邻字符代价为1,求最小代价使得字符串不含"VK"子串.n<=75. [算法]动态规划 [题解 ...

  5. Node.js的开源博客系统Ghost搭建教程

    准备工作 Node.js版本:0.10.x.0.12.x.4.2.x.安装步骤可参考:Node.js环境搭建 Ghost版本:0.7.4:中文集成版(33.6M),中文标准版(3.39M),英文原版( ...

  6. 【洛谷 P2553】 [AHOI2001]多项式乘法(FFT)

    题目链接 简单处理一下输入,\(fft\)模板题. #include <cstdio> #include <cmath> #include <algorithm> ...

  7. 【leetcode 简单】第三十九题 Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...

  8. 2017ACM暑期多校联合训练 - Team 3 1005 RXD and dividing

    题目链接 Problem Description RXD has a tree T, with the size of n. Each edge has a cost. Define f(S) as ...

  9. 如何写出优雅的js以及js特殊技巧

    由于代码和解释都写在了github的readme内部,这里就直接附上github:https://github.com/jiangzhenfei/pretty-js/tree/master

  10. vue.js devtools-------调试vue.js的开发者插件

    vue.js devtools插件: 作用: 以往我们在进行测试代码的时候,直接在console进行查看,其实这个插件雷同于控制台,只不过在vue里面,将需要查看的数据存放在一个变量里面了~ 效果图: ...