CF808G Anthem of Berland
大概知道两种 DP 方法。(
方法一:
设
复杂度
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[1<<14],*p1=buf,*p2=buf;
#define GetC() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++)
struct Ios{}io;
template <typename _tp>
Ios &operator >>(Ios &in,_tp &x){
x=0;int w=0;char c=GetC();
for(;!isdigit(c);w|=c=='-',c=GetC());
for(;isdigit(c);x=x*10+(c^'0'),c=GetC());
if(w) x=-x;
return in;
}
const int N=1e5+5,inf=1e9;
char s[N],t[N];
int nxt[N];
int f[N],g[N];
int n,m;
bool check(int i,int j=1){
for(int x=0;x<m;++x){
if(s[i+x]!=t[j+x]&&s[i+x]!='?') return false;
}
return true;
}
int main(){
scanf("%s%s",s+1,t+1);
n=strlen(s+1),m=strlen(t+1);
int j=0;
for(int i=2;i<=m;++i){
while(j&&t[j+1]!=t[i]) j=nxt[j];
if(t[j+1]==t[i]) ++j;
nxt[i]=j;
}
for(int i=m;i<=n;++i){
g[i]=-inf;
if(check(i-m+1)){
g[i]=f[i-m]+1;
for(int j=nxt[m];j;j=nxt[j]){
g[i]=max(g[i],g[i-(m-j)]+1);
}
}
f[i]=max(f[i-1],g[i]);
}
printf("%d\n",f[n]);
return 0;
}
方法二:
设
对于 ?
枚举填什么。
转移时用 KMP 自动机 实现即可。复杂度
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】