[POI2005]Sza-Template

Description
Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段作为模版. 然后将模版重复喷涂到相应的位置后就得到了他想要的字符序列.一个字符可以被喷涂很多次,但是一个位置不能喷涂不同的字符.做一个模版很费工夫,所以他想要模版的长度尽量小,求最小长度是多少.

Input
输入一行最多不超过500 000 个最少1个小写字符.

Output
一个长度表示模版最小的长度.

Sample Input
ababbababbabababbabababbababbaba

Sample Output
8


这题我们考虑一个dp的做法,设(f[i])表示填涂到第(i)位所用次数,然后(f[i])只可能有两个取值:(i,f[Nxt[i]]),因为想覆盖(i)至少要先覆盖(Nxt[i]),那什么时候可以由(f[Nxt[i]])转移过来呢?

因为(i)后面几个字符为(Nxt[i]),所以我们可以在最后接上(Nxt[i])个字符,所以转移的充要条件为存在一个(j),使得(f[j]=f[Nxt[i]]),且(i-Nxt[i]leqslant j)

然后开个桶存一下就好

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1;char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)    putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e6;
int Nxt[N+10],f[N+10],h[N+10];
char s[N+10];
int main(){
	scanf("%s",s+1);
	int n=strlen(s+1); f[1]=h[1]=1;
	for (int i=2,j=0;i<=n;i++){
		while (j&&s[i]!=s[j+1])	j=Nxt[j];
		if (s[i]==s[j+1])	j++;
		Nxt[i]=j,f[i]=i;
		if (h[f[Nxt[i]]]>=i-Nxt[i])	f[i]=f[Nxt[i]];
		h[f[i]]=i;
	}
	printf("%d
",f[n]);
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/10484581.html