Luogu3435 [POI2006]OKR-Periods of Words (KMP)
\(next\)应用,将原串视作最长前缀复制后的子串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
int nxt[1000007];
char str[1000007];
int main(){
int len;
io >> len;
scanf("%s", str);
long long ans = 0;
int i = 0, j = -1;
nxt[0] = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
nxt[++i] = ++j;
while(nxt[nxt[i]] > 0) nxt[i] = nxt[nxt[i]];
if(nxt[i] > 0){
ans += i -nxt[i];
}
}
else
j = nxt[j];
}
printf("%lld", ans);
return 0;
}

Luogu3435 [POI2006]OKR-Periods of Words (KMP)的更多相关文章
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- POJ 2406 Power Strings(KMP)
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- LightOJ 1258 Making Huge Palindromes(KMP)
题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...
- codeM编程大赛E题 (暴力+字符串匹配(kmp))
题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...
- 经典串匹配算法(KMP)解析
一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...
- URAL 1732 Ministry of Truth(KMP)
Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...
- Leetcode28--->字符串的匹配(KMP)
题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...
- 题解0012:剪花布条(KMP)
信奥一本通1465 KPM例题 题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1465 题目描述:给出花布条和小饰条(字符串),求花布条中能剪 ...
- BZOJ 3796 Mushroom追妹纸 哈希+二分(+KMP)
先把两个串能匹配模式串的位置找出来,然后标记为$1$(标记在开头或末尾都行),然后对标记数组求一个前缀和,这样可以快速查到区间内是否有完整的一个模式串. 然后二分子串(答案)的长度,每次把长度为$md ...
随机推荐
- MySQL之事务隔离级别和MVCC
事务隔离级别 事务并发可能出现的问题 脏写 事务之间对增删改互相影响 脏读 事务之间读取其他未提交事务的数据 不可重复读 一个事务在多次执行一个select读到的数据前后不相同.因为被别的未提交事务修 ...
- 关于JNPF3.4版本的三大改变,你真的了解了吗?
- 内网穿透frp教程 windows远程桌面连接
鉴于ngrok不是特别好用 昨天又发现frp这个神器 在管理端还有图形界面十分友好 话不多说开始 准备工作 1.一个域名 2.一台服务器 一.域名与服务器 域名和服务器直接买就好咯 价格不高 一定要在 ...
- 使用 content-visibility 优化渲染性能
最近在业务中实际使用 content-visibility 进了一些渲染性能的优化. 这是一个比较新且有强大功能的属性.本文将带领大家深入理解一番. 何为 content-visibility? co ...
- BUUCTF-大白
大白 使用16进制打开没发现什么异常的,根据提示来看图片可能没有显示完整 将第七位修改为02即可正常查看图片.
- zabbix-5.0自动发现
1. 安装zabbix5.0 rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.no ...
- 不可思议的返回功能——python
今天给大家分享 3 个比较冷门的知识.教程点这(https://jq.qq.com/?_wv=1027&k=zLK3I0M5) 第一个:神奇的字典键 (https://jq.qq.com/?_ ...
- vue在Docker上运行
Dockerfile # 设置基础镜像 FROM nginx:latest # 定义作者 MAINTAINER test # 将dist文件中的内容复制到 /etc/nginx/html/ 这个目录下 ...
- vmstate 命令详解2022
vmstat 是一个查看虚拟内存(Virtual Memory)使用状况的工具,但是怎样通过 vmstat 来发现系统中的瓶颈呢? 1. 使用vmstat 使用前我们先看下命令介绍及参数定义 Usag ...
- kubectl 最新常用命令 --V1.24版本
Kubectl 自动补全 BASH source <(kubectl completion bash) # 在 bash 中设置当前 shell 的自动补全,要先安装 bash-completi ...