正题

题目链接:https://www.luogu.com.cn/problem/P3426


题目大意

给出一个长度为\(n\)的字符串\(s\),求一个长度最小的字符串\(t\)使得\(s\)所有\(t\)和\(t\)匹配的位置能覆盖串\(s\)。

\(1\leq n\leq 5\times 10^5\)


解题思路

首先答案肯定是原串的一个\(border\),设\(f_i\)表示前缀\(s_{1\sim i}\)的答案。

考虑如何转移,首先\(f_i\)至多是\(i\),然后考虑如果有一个串\(t\)能够覆盖\(s_{1\sim nxt_i}\)那么才有可能能覆盖\(s_{1\sim i}\)。(因为如果覆盖大于\(nxt_i\)显然不可能覆盖整个串,不然就至少需要覆盖到\(s_{1\sim nxt_i}\))。

考虑什么时候\(f_i\)能够取到\(f_{nxt_i}\)。首先我们可以表示出\(s_{1,nxt_i}\)假设我们上次覆盖的位置是\(j\in[nxt_i,i]\),那么需要有\(f_{j}=f_{nxt_i}\)(即取到同一个\(border\)),然后要求\(j\geq i-nxt_i\)(这样就可以用\(s_{1,nxt_i}\)覆盖剩下的)。

时间复杂度\(O(n)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5+10;
int n,f[N],nxt[N],ls[N];
char s[N];
int main()
{
scanf("%s",s+1);n=strlen(s+1);
for(int i=2,j=0;i<=n;i++){
while(j&&s[j+1]!=s[i])j=nxt[j];
j+=(s[i]==s[j+1]);nxt[i]=j;
}
for(int i=1;i<=n;i++){
f[i]=i;
if(i-ls[f[nxt[i]]]<=nxt[i])
f[i]=f[nxt[i]];
ls[f[i]]=i;
}
printf("%d\n",f[n]);
return 0;
}

P3426-[POI2005]SZA-Template【KMP】的更多相关文章

  1. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  2. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  3. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  5. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  6. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  7. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  8. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  9. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  10. 【POJ3461】【KMP】Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

随机推荐

  1. SpringCloud升级之路2020.0.x版-22.Spring Cloud LoadBalancer核心源码

    本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 经过上一节的详细分 ...

  2. Failed to start LSB: Bring up/down错误解决方法

    很多朋友在使用centos7系统时,有时候需要分配多个IP地址,这就涉及到修改网卡配置,但是在修改完网卡配置时,重启网络服务时会出现"Failed to start LSB: Bring u ...

  3. dubbo(一)

    1. Introduction 参考链接:https://www.baeldung.com/dubbo Dubbo is an open-source RPC and microservice fra ...

  4. springgateway

    SpringGateAway: 先进行鉴权,然后进行路由,日志什么等等

  5. ES6 class——getter setter音乐播放器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. ES6扩展——数值扩展

    1.0o代表八进制 0b代表二进制 ,通过Number()可转为10进制: //0o 0O octanary八进制 //0b 0B binary二进制 console.log(0o16); //14 ...

  7. 学习小计: Kaggle Learn Time Series Modeling

    ARIMA模型,参数含义参考:https://www.cnblogs.com/bradleon/p/6827109.html from statsmodels.tsa.arima_model impo ...

  8. VSCode 在.vscode/launch.json中设置启动时的参数

    如下脚本设置启动参数,如题,在.vscode/launch.json文件中,红色部分设置运行参数 { // Use IntelliSense to learn about possible attri ...

  9. 运输层协议:TCP连接建立与释放

    TCP的特点 面向连接:TCP是面向连接的运输层协议,通过TCP发送数据需要先建立连接,通信结束后需要释放连接 可靠传输:TCP实现了可靠传输,使得数据能够无丢失.无差错.不重复地到达接收端 面向字节 ...

  10. jvm学习笔记:栈帧

    栈帧内的数据结构 局部变量表(Local Variables):记录非静态方法的this指针.方法参数.局部变量 操作数栈(Operand Stack):用于计算的栈结构 动态链接(Dynamic L ...