2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP)

https://www.luogu.com.cn/problem/P3435

题意:

对于一个仅含小写字母的字符串 a,p 为 a 的前缀且 p不等于a,那么我们称 p 为 a的 proper 前缀。

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

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

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

分析:

https://ofnoname.blog.luogu.org/solution-p3435

nexti数组存的是最长的 前缀与后缀 的公共前缀!

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int N=1e6+10;
typedef long long ll;
ll ans;
int n,nexti[N];
char s[N]; int main(){
cin>>n;
scanf("%s",s);
int k=-1;
nexti[0]=-1;
for(int i=0;i<n;){
while(s[i]!=s[k]&&k!=-1)k=nexti[k];
if(s[i]==s[k]||k==-1)nexti[++i]=++k;
}
//for(int i=1;i<=n;i++)cout<<nexti[i]<<" ";cout<<endl;
for(int i=2,j=2;i<=n;i++,j=i){
while(nexti[j])j=nexti[j];
if(nexti[i])nexti[i]=j;
ans+=i-j;
}
cout<<ans;
return 0;
}

最新文章

  1. RESTful API 设计最佳实践
  2. NUOJ 88
  3. ORACLE AWR报告生成过程出现多个实例记录分析
  4. 20145304 Java第八周学习报告
  5. [转]ASP.NET MVC 入门6、TempData
  6. Delphi:窗体自适应屏幕分辨率(根据预设值的比例改变)
  7. asp.net根据模版生成Word小记
  8. 深入理解 JavaScript 中的 replace 方法(转)
  9. Hibternate框架笔记
  10. jQuery——动态给表格添加序号
  11. Java动态代理(一)
  12. MyX5TbsDemo【体验腾讯浏览服务Android SDK (完整版)】
  13. 一个box-sizing: border-box和felx混合使用中遇到的问题
  14. git clean 删除忽略文件 和 未被跟踪文件及文件夹
  15. Spring类型转换(Converter)
  16. day11 函数的位置形参,位置实参,可变长位置形参,关键字形参
  17. 一个十年IT从业者的职场感言:为什么不要自称是“程序员”
  18. Bash - 趣味Shell
  19. Python 文件学习笔记
  20. Python的多线程和多进程

热门文章

  1. 痞子衡嵌入式:IAR内部C-SPY调试组件配套宏文件(.mac)用法介绍
  2. JDK API文档_1.6.0 中文版
  3. uniapp小程序图片前端压缩上传
  4. 数据库篇:mysql锁详解
  5. SpringBoot starter 作用在什么地方?
  6. mac下的phpstorm增加xdebug调试
  7. MySQL 支持事务吗?
  8. 学习 Haproxy (四)
  9. 介绍一项让 React 可以与 Vue 抗衡的技术
  10. 【Weex笔记】-- Animate.css的使用