洛谷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 ...
随机推荐
- WebApi连接数据库
首先在web.config中加上语句连接语句: <connectionStrings> <add name="conn" connectionString=&qu ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(三)
属性配置 1.配置application.properties文件 配置web访问端口和context path server.port = 8081 server.servlet.context-p ...
- 安装HDP时的报错信息
1,安装ambari时报错:Bootstrap process timed out. It will be destroyed. 报错原因:/etc/sudoers文件中未设置免密权限 解决办法:ha ...
- 千里之行始于足下,node.js 资源中文导航
响应@jiyinyiyong 号召,cnodjs 好的资源蛮多的,的确欠缺分类,在一群FAQ中,的确很容易沉下去,根据自己对node.js的理解,做成一个资源导航,PS:如果觉得合适,希望能够合并的c ...
- 不修改代码优化 ASP.NET 网站性能的一些方法
本文将介绍一些方法用于优化ASP.NET网站性能,这些方法都是不需要修改程序代码的.它们主要分为二个方面:1. 利用ASP.NET自身的扩展性进行优化.2. 优化IIS设置. 配置OutputCach ...
- AngularJs几种服务区别
下面说说这几种函数之间的区别: 函数 定义 适合场景 provider(name, Object OR constructor() ) 一个可配置的.有复杂逻辑的服务.如果你传递了一个对象,那么它应该 ...
- 【译】Linux提权基础
英文原文: Basic Linux Privilege Escalation 在开始之前,我想指出 - 我不是专家. 据我所知,在这个巨大的领域没有一个“魔法”的答案. 这只是我的发现,写出来,共享而 ...
- Linux必备工具Tmux
之前介绍了Linux的Screen命令,今天介绍一个更为强大的终端工具Tmux. Tmux 是一个用于在一个终端窗口中运行多个终端会话的工具.它基本能替代nohup以及screen,甚至比它们更为强大 ...
- UIAutomation Diagram
- nmon的安装和使用
1.下载nmon https://zh.osdn.net/projects/sfnet_nmon/downloads/nmon_x86_64_rhel6/ 2../nmon_x86_64_rhel6 ...