跳到正文

题解

AT_abc462_f [ABC462F] More ABC 题解

yangshengyue约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月16日
收录
2026年6月16日
更新
2026年11月15日
篇幅
约 7 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

发现 ABC 的 F 都好爱考字符串动态规划。

题面翻译

给定字符串 SS,将 SS 中的若干字符替换,让 SS 中的 ABC\texttt{ABC} 字符组合数量比原来增加 kk 个,求最少替换数。

好 DP 的 DP。

考虑动态规划

由于 k≤10k\le10,最后时间复杂度肯定是一个类似 O(nk)O(nk) 这样的动态规划。

先考虑无解情况。整个字符串最多能容纳多少个不重叠的 ABC\texttt{ABC}?每 33 个字符最多 11 个,上限为 ⌊∣S∣3⌋\lfloor \frac{|S|}{3} \rfloor。 设原串已有 orig cnt\text{orig cnt} 个 ABC,若  orig cnt +k>⌊∣S∣3⌋\text{ orig cnt }+k>\lfloor \frac{|S|}{3} \rfloor,直接无解。

观察一下,匹配状态只有 33 种,我们只关心当前后缀匹配 ABC\texttt{ABC} 到哪一步。

当 stst 为 22 且当前字符选为 C\texttt C 时,会新生成 11 个 ABC\texttt{ABC} 字串。

再观察,kk 很小,我们只需要记录累计新增了多少个 ABC\texttt{ABC},最多存到 kk 即可,超过 kk 不用记录。

可以导出 DP 定义了。

令 dpi,j,stdp_{i,j,st} 为处理到第 ii 位时,累计净新增 jj 个 ABC\texttt{ABC}、当前后缀匹配状态为 stst,所需的最小修改代价。初始设全零的位置为 00,其余设为无穷大。

状态转移可以遍历上一层所有合法状态,枚举当前位置 ii 和改成的字符 cc,计算代价 costcost,新匹配状态 nsns 和本次新增 formedformed。

对于之前位置 ii,我们枚举 44 个候选字符 A\texttt A、B\texttt B、C\texttt C、X\texttt X 就行,其中 X\texttt X 代表非 A\texttt A 或 B\texttt B 或 C\texttt C 的任意大写字母。

计算修改代价 costcost 时, 选 A\texttt A 或 B\texttt B 或 C\texttt C,仅当 SiS_i 等于该字符 costcost 为 00 ,否则为 11; 选 X\texttt X,当 SiS_i 不是 A\texttt A 或 B\texttt B 或 C\texttt C 时 costcost 为 00,否则为 11。

新匹配状态 nsns 和本次新增 formedformed 要简单讨论一下。

若 st=2st=2 且 c=Cc=\texttt{C},拼接出了完整 ABC\texttt {ABC} ,formedformed 设为 11,nsns 设为 00。

若 c=Cc=\texttt{C},设 nsns 为 11,无新增,formedformed 设为 00。

若 st=1st=1 且 c=Cc=\texttt{C},拼接了 AB\texttt {AB},新状态 nsns 为 22,无新增,formedformed 设为 00。

其余情况设 nsns 为 00,formedformed 为 00。

如果令 was orig\text {was orig} 为原串中 ii 位置是否是 ABC\texttt{ABC} 的结束位(原串自带的 ABC\texttt{ABC},会抵消新增计数), 净新增数量 nj=j+formed−was orignj=j+formed−\text{was orig}。

解释一下,本次新造 formedformed 个 ABC\texttt{ABC},但原串这里本来就有 was orig\text{was orig} 个,净增加为 formed−was origformed−\text{was orig}。

当前枚举仅当 0≤nj≤k0 \le nj\le k 时合法,转移公式为 dpi,nj,ns=min⁡(dpi,nj,ns,dpi−1,j,st+cost)dp_{i,nj,ns}= \min (dp_{i,nj,ns},dp_{i-1,j,st}+cost)。

你可以滚动数组。

最后贴上代码

AC code
C++
#include<bits/stdc++.h>
#define ll long long
#define re register
using namespace std;
const int INF=1e9;
int T,n,k,orig_cnt;
char s[300005];
bool orig_end[300005];
int dp[2][12][3];
inline void ckmin(re int &a,re int b){if(b<a)a=b;}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>T;
	while(T--){
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
		cin.getline(s+1,300004);
		n=strlen(s+1);
		cin>>k;
		
		re int max_possible=n/3;
		orig_cnt=0;
		for(re int i=1;i<=n;i++)orig_end[i]=false;
		for(re int i=3;i<=n;i++){
			if(s[i-2]=='A'&&s[i-1]=='B'&&s[i]=='C'){
				orig_end[i]=true;
				orig_cnt++;
			}
		}
		if(orig_cnt+k>max_possible){
			cout<<-1<<"\n";
			continue;
		}
		for(re int j=0;j<=k;j++)
			for(re int st=0;st<3;st++)
				dp[0][j][st]=INF;
		dp[0][0][0]=0;
		re int cur=0,nxt=1;
		const char chars[]={'A','B','C','X'};
		for(re int i=1;i<=n;i++){
			for(re int j=0;j<=k;j++)
				for(re int st=0;st<3;st++)
					dp[nxt][j][st]=INF;
			for(re int ci=0;ci<4;ci++){
				re char c=chars[ci];
				re int cost;
				if(c=='X')cost=(s[i]!='A'&&s[i]!='B'&&s[i]!='C')?0:1;
				else cost=(s[i]==c)?0:1;
				for(re int j=0;j<=k;j++){
					for(re int st=0;st<3;st++){
						if(dp[cur][j][st]>=INF)continue;
						re int formed=0,ns=0;
						if(st==2&&c=='C'){formed=1;ns=0;}
						else if(c=='A'){ns=1;}
						else if(st==1&&c=='B'){ns=2;}
						else{ns=0;}
						re int was_orig=orig_end[i]?1:0;
						re int nj=j+formed-was_orig;
						if(nj>=0&&nj<=k)ckmin(dp[nxt][nj][ns],dp[cur][j][st]+cost);
					}
				}
			}
			cur^=1;nxt^=1;
		}
		re int ans=INF;
		for(re int st=0;st<3;st++)ckmin(ans,dp[cur][k][st]);
		if(ans>=INF)cout<<-1<<"\n";
		else cout<<ans<<"\n";
	}
	return 0;
}
原文