P3002 [USACO10DEC] Threatening Letter G

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

首先考虑一个显然的 dp,设 fi 表示最后一刀切在 i 上,并将 1i 全部剪出的最小刀数。转移显然是 fi=min0j<i,tj+1isfj+1,其中 tj+1i 表示字符串 t 的子串 [j+1,i]ts 表示 ts 的子串。

转移显然是可以线段树优化掉的,关键求 j 的范围。

显然若 tj,is 那么 tj+1,is,所以本质上是求第一个满足 tj,isj

子串问题考虑后缀数组,将两个串拼接到一起并用奇怪字符隔开,那么我们可以二分 j,用后缀数组来验证子串关系。但其实将 s 串和 t 串翻转之后就不需要二分了,根据后缀 ti,ms 的后缀的最长公共前缀就能得出答案。

众所周知,对于一个后缀来说,在 sa 数组上更近的后缀的 LCP 肯定比更远的后缀的 LCP 长度更大,所以我们只需要对于每个后缀求出两边距离其最近且是 s 的后缀的后缀,求两者 LCP 的最大值即可。

由于我不会 SA,所以求 sa 部分采用二分哈希实现,时间复杂度 O(nlog2n),需要略微卡常。

点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=1e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const int mod[2]={998244353,1000000007},base=41;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
int len;
int mi[maxn][2];
char s[maxn],t[maxn],str[maxn];
namespace Fakesa{
	namespace hashtable{
		int f[maxn][2];
		inline void init(){
			mi[0][0]=mi[0][1]=1;rep(i,1,len)rep(j,0,1)mi[i][j]=mi[i-1][j]*base%mod[j];
			rep(i,1,len)rep(j,0,1)f[i][j]=(f[i-1][j]*base%mod[j]+str[i]-'A')%mod[j];
		}
		inline int qry(int l,int r,int o){
			return (f[r][o]-f[l-1][o]*mi[r-l+1][o]%mod[o]+mod[o])%mod[o];
		}
		inline bool isequal(int l1,int r1,int l2,int r2){
			return qry(l1,r1,0)==qry(l2,r2,0)&&qry(l1,r1,1)==qry(l2,r2,1);
		}
	}using namespace hashtable;
	int sa[maxn],rk[maxn];
	int hi[maxn];
	inline int lcp(int x,int y){ 
		int l=1,r=min(len-x+1,len-y+1),res=0;
		while(l<=r){
			int mid=l+r>>1;
			if(isequal(x,x+mid-1,y,y+mid-1))l=mid+1;
			else r=mid-1,res=mid;
		}
		return res-1;
	}
	bool cmp(int x,int y){
		int res=lcp(x,y);
		if(res==-1)return x>y;
		return str[x+res]<str[y+res];
	}
	inline void SA(){
		init();
		rep(i,1,len)sa[i]=i;
		sort(sa+1,sa+len+1,cmp);
		rep(i,1,len)rk[sa[i]]=i;
		rep(i,2,len)hi[i]=lcp(sa[i-1],sa[i]);
		rep(i,2,len)if(hi[i]==-1)hi[i]=len-max(sa[i-1],sa[i])+1;
	}
}using namespace Fakesa;
namespace ST{
	int f[maxn][20];
	inline void init(){
		rep(i,1,len)f[i][0]=hi[i];
		int M=log2(len);
		rep(j,1,M)rep(i,1,len-(1<<j)+1)f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	}
	inline int query(int l,int r){
		int p=log2(r-l+1);
		return min(f[l][p],f[r-(1<<p)+1][p]);
	}
}using namespace ST;
int pre[maxn],sec[maxn];
int dp[maxn];
inline void solve_the_problem(){
	cin>>n>>m;
	rep(i,1,n)cin>>s[i],str[n-i+1]=s[i];str[n+1]='Z'+1;
	rep(i,1,m)cin>>t[i],str[n+m-i+2]=t[i];
	len=n+m+1;
	SA();
	ST::init();
	rep(i,1,len){
		pre[i]=pre[i-1];
		if(sa[i]<=n)pre[i]=i;
	}
	sec[len+1]=len+1;
	per(i,len,1){
		sec[i]=sec[i+1];
		if(sa[i]<=n)sec[i]=i;
	}
	rep(i,1,m){
		int q=1,p=rk[n+m-i+2];
		if(pre[p])q=max(q,query(pre[p]+1,p));
		if(sec[p]!=len+1)q=max(q,query(p+1,sec[p]));
		dp[i]=dp[i-q]+1;
	}
	cout<<dp[m];
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	IOS;ITIE;OTIE;int _=1;while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/17999278

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示