[POI2006] OKR-Periods of Words题解

题面翻译

对于一个仅含小写字母的字符串 aaa,ppp 为 aaa 的前缀且 p≠ap\ne ap=a,那么我们称 ppp 为 aaa 的 proper 前缀。

规定字符串 QQQ(可以是空串)表示 aaa 的周期,当且仅当 QQQ 是 aaa 的 proper 前缀且 aaa 是 Q+QQ+QQ+Q 的前缀。

例如 ababab 的一个周期,因为 ababab 的 proper 前缀,且 ababab+ab 的前缀。

求给定字符串所有前缀的最大周期长度之和。

题目描述

A string is a finite sequence of lower-case (non-capital) letters of the English alphabet. Particularly, it may be an empty sequence, i.e. a sequence of 0 letters. By A=BC we denotes that A is a string obtained by concatenation (joining by writing one immediately after another, i.e. without any space, etc.) of the strings B and C (in this order). A string P is a prefix of the string !, if there is a string B, that A=PB. In other words, prefixes of A are the initial fragments of A. In addition, if P!=A and P is not an empty string, we say, that P is a proper prefix of A.

A string Q is a period of Q, if Q is a proper prefix of A and A is a prefix (not necessarily a proper one) of the string QQ. For example, the strings abab and ababab are both periods of the string abababa. The maximum period of a string A is the longest of its periods or the empty string, if A doesn’t have any period. For example, the maximum period of ababab is abab. The maximum period of abc is the empty string.

Task Write a programme that:

reads from the standard input the string’s length and the string itself,calculates the sum of lengths of maximum periods of all its prefixes,writes the result to the standard output.

输入格式

In the first line of the standard input there is one integer kkk (1≤k≤10000001\le k\le 1\ 000\ 0001≤k≤1 000 000) - the length of the string. In the following line a sequence of exactly kkk lower-case letters of the English alphabet is written - the string.

输出格式

In the first and only line of the standard output your programme should write an integer - the sum of lengths of maximum periods of all prefixes of the string given in the input.

样例 #1

样例输入 #1
8
babababa
样例输出 #1
24

思路

题意大致翻译过来就是让你在字符串a中寻找一个最大的前缀i,让这个i重复两遍能够覆盖住原来前缀i的前缀,相当于就是求前缀的前缀,一直递推到长度为0.

那么毫无疑问,我们需要运用到KMP,去使用nex数组的性质:前缀i的长度为next[i]的前缀和后缀是相等的。

可能有那么亿点绕口,

那么我们来举一个狸子

相信大家都能看懂啊,那么我们图中蓝色部分就是一个周期了,next[next[j]]就是前缀的前缀,而我们要想使周期最大,就要使前缀越小,且我们要使i=j,这样才能求道最小的前缀,那么我们就需要递推到最极端的情况,就是j>0的时候,这时候前缀最小,周期最大。

代码实现

先求出next数组

对于每个前缀i,令j=i,然后在j>0的情况下令j=next[j](递推过程)(前缀的前缀),最小的j就是答案,此时ans+=i−j(很明显,图上就能看出来)。

一个优化:求出j以后,令j=next[i](luogu不要过不了),这样能加快递归速度(相当于记忆化了)

CODE

#include <bits/stdc++.h>
using namespace std;
char s[1000001];
long long n,nex[1000001];
void getnext(char s[],int len) {int i=0,j=-1;nex[0]=-1;while(i<len) {if(j==-1||s[i]==s[j]) {j++;i++;nex[i]=j;}else j=nex[j];}
}
int main() {cin>>n;scanf("%s",s);getnext(s,n);long long ans=0;for(int i=1; i<=n; i++) {int j=i;while(nex[j]) {j=nex[j];}if(nex[i]!=0) {nex[i]=j;}ans+=i-j;}cout<<ans;return 0;
}

[POI2006] OKR-Periods of Words题解相关推荐

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

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

  2. 【题解】[POI2006」Tet-Tetris 3D

    sol: 考虑用树套树维护二维信息. 这里标记比较复杂,所以不好下传,考虑 标记永久化. 每次修改区间的时候,把遍历到的点都修改 mx ,对于完全覆盖的区间则同时修改 tag 和 mx .(这里的修改 ...

  3. codeforces 314 (Div 1) 题解

    文章目录 前言 A Sereja and Contest key 题意 思路 B Sereja and Periods key 题意 思路 代码 C Sereja and Subsequences k ...

  4. [POI2006] OKR-period of words

    传送门 - > \(bzoj 1511\) 题目描述 A string is a finite sequence of lower-case (non-capital) letters of t ...

  5. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  6. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  7. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  8. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  9. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  10. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

最新文章

  1. CocoaPods的安装失败方法
  2. 个人开发者帐号+wireless install 实现非app store程序的在线更新功能
  3. Spring 框架的AOP之注解的方式
  4. 【docker】【Gitlab】gitlab中clone项目时,IP地址是一串数字(内网Gitlab的IP地址不正确)的问题解决...
  5. Python的oop概述
  6. boost::mp11::mp_less相关用法的测试程序
  7. 创作一个数字人,总共分几步?(下)
  8. string类的各种函数用法
  9. php安装编译时 configure: error: Cannot find OpenSSL's evp.h
  10. paddleOCR常见问题(2)
  11. springboot 学习笔记【1】开发第一个spring boot应用
  12. 浪曦struts2学习笔记3
  13. wordpress如何获取文章图片及图片路径
  14. 怎么样恢复移动硬盘格式化的数据呢?
  15. 关于蜕变测试文献REST ful 的Web API的翻译与思考
  16. ETM地形编辑DEMO运行时出错问题
  17. 详解动态规划算法(Python实现动态规划算法典型例题)
  18. TabLayout使用介绍
  19. 流形间的映射(拉回映射与推前映射)及根据其定义的协变矢量和逆变矢量;切空间与余切空间
  20. 题目65:一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格。

热门文章

  1. 交互式电子白板有哪些功能
  2. Java学习1——计算机基础知识
  3. aurora协议学习之时钟补偿
  4. 7654:等差数列末项计算
  5. hadoop完全分布式教程网页
  6. 记录:英文参考文献格式
  7. 英语中提醒注意安全句子
  8. Preempt_RT Linux技术文档(一)技术基础(中英文对照)
  9. linux 内核 禁止抢占,内核抢占实现(preempt)
  10. 《The Elder Scrolls V: Skyrim》百般冷门却强力职业