版本比较
AT_abc462_f [ABC462F] More ABC 题解
正文
增加 83 行删除 18 行
123345678910111291013141512161714181916202122232024252627242829302728313233343532363734383936404138424340444546434748454950515253505455565758594 行未改动646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129发现 ABC 的 F 都好爱考字符串动态规划。
## 题面翻译给定字符串 $S$,将 $S$ 中的若干字符替换,让 $S$ 中的 ABC 字符组合数量比原来增加 $k$ 个,求最少替换数。
给定字符串 $S$,将 $S$ 中的若干字符替换,让 $S$ 中的 $\texttt{ABC}$ 字符组合数量比原来增加 $k$ 个,求最少替换数。
好 DP 的 DP。
## 考虑动态规划
由于 $k\le10$,最后时间复杂度肯定是一个类似 $O(nk)$ 这样的动态规划。
先考虑无解情况。整个字符串最多能容纳多少个不重叠的 ABC ?每 3 个字符最多 1 个,上限为 $\lfloor \frac{|S|}{3} \rfloor$。设原串已有 $\text{orig cnt}$ 个 ABC,若 $\text{ orig cnt }+k>\lfloor \frac{|S|}{3} \rfloor$,直接无解。先考虑无解情况。整个字符串最多能容纳多少个不重叠的 $\texttt{ABC}$?每 $3$ 个字符最多 $1$ 个,上限为 $\lfloor \frac{|S|}{3} \rfloor$。设原串已有 $\text{orig cnt}$ 个 ABC,若 $\text{ orig cnt }+k>\lfloor \frac{|S|}{3} \rfloor$,直接无解。
观察一下,匹配状态只有 3 种,我们只关心当前后缀匹配 ABC 到哪一步。…观察一下,匹配状态只有 $3$ 种,我们只关心当前后缀匹配 $\texttt{ABC}$ 到哪一步。
当 $st$ 为 2 且当前字符选为 C 时,会新生成 1 个 ABC 子串。当 $st$ 为 $2$ 且当前字符选为 $\texttt C$ 时,会新生成 $1$ 个 $\texttt{ABC}$ 字串。
再观察,$k$ 很小,我们只需要记录累计新增了多少个 ABC,最多存到 $k$ 即可,超过 $k$ 不用记录。再观察,$k$ 很小,我们只需要记录累计新增了多少个 $\texttt{ABC}$,最多存到 $k$ 即可,超过 $k$ 不用记录。
可以导出 DP 定义了。
令 $dp_{i,j,st}$ 为处理到第 $i$ 位时,累计净新增 $j$ 个 ABC、当前后缀匹配状态为 $st$,所需的最小修改代价。初始设全零的位置为 0,其余设为无穷大。令 $dp_{i,j,st}$ 为处理到第 $i$ 位时,累计净新增 $j$ 个 $\texttt{ABC}$、当前后缀匹配状态为 $st$,所需的最小修改代价。初始设全零的位置为 $0$,其余设为无穷大。
状态转移可以遍历上一层所有合法状态,枚举当前位置 $i$ 和改成的字符 $c$,计算代价 $cost$,新匹配状态 $ns$ 和本次新增 $formed$。
对于之前位置 $i$,我们枚举 4 个候选字符 A、B、C、X 就行,其中 X 代表非 A 或 B 或 C 的任意大写字母。对于之前位置 $i$,我们枚举 $4$ 个候选字符 $\texttt A$、$\texttt B$、$\texttt C$、$\texttt X$ 就行,其中 $\texttt X$ 代表非 $\texttt A$ 或 $\texttt B$ 或 $\texttt C$ 的任意大写字母。
计算修改代价 $cost$ 时,选 A 或 B 或C,仅当 s[i] 等于该字符 $cost$ 为 0 ,否则为 1;选 X,当 s[i] 不是 A 或 B 或 C 时 $cost$ 为 0,否则为 1。选 $\texttt A$ 或 $\texttt B$ 或 $\texttt C$,仅当 $S_i$ 等于该字符 $cost$ 为 $0$ ,否则为 $1$;选 $\texttt X$,当 $S_i$ 不是 $\texttt A$ 或 $\texttt B$ 或 $\texttt C$ 时 $cost$ 为 $0$,否则为 $1$。
新匹配状态 $ns$ 和本次新增 $formed$ 要简单讨论一下。
若 $st=2$ 且 $c=\texttt{C}$,拼接出了完整 ABC ,$formed$ 设为 1,$ns$ 设为 0。若 $st=2$ 且 $c=\texttt{C}$,拼接出了完整 $\texttt {ABC}$ ,$formed$ 设为 $1$,$ns$ 设为 $0$。
若 $c=\texttt{C}$,设 $ns$ 为 1,无新增,$formed$ 设为 0。若 $c=\texttt{C}$,设 $ns$ 为 $1$,无新增,$formed$ 设为 $0$。
若 $st=1$ 且 $c=\texttt{C}$,拼接了 AB,新状态 $ns$ 为 2,无新增,$formed$ 设为 0。若 $st=1$ 且 $c=\texttt{C}$,拼接了 $\texttt {AB}$,新状态 $ns$ 为 $2$,无新增,$formed$ 设为 $0$。
其余情况设 $ns$ 0,$formed$ 为 0。其余情况设 $ns$ 为 $0$,$formed$ 为 $0$。
如果令 $\text {was orig}$ 为原串中 $i$ 位置是否是 ABC 的结束位(原串自带的 ABC,会抵消新增计数),如果令 $\text {was orig}$ 为原串中 $i$ 位置是否是 $\texttt{ABC}$ 的结束位(原串自带的 $\texttt{ABC}$,会抵消新增计数),净新增数量 $nj=j+formed−\text{was orig}$。
解释一下,本次新造 $formed$ 个 ABC,但原串这里本来就有 $\text{was orig}$ 个,净增加为 $formed−\text{was orig}$。解释一下,本次新造 $formed$ 个 $\texttt{ABC}$,但原串这里本来就有 $\text{was orig}$ 个,净增加为 $formed−\text{was orig}$。
当前枚举仅当 $0 \le nj\le k $ 时合法,转移公式为 $dp_{i,nj,ns}=min(dp_{i,nj,ns},dp_{i-1,j,st}+cost)$。当前枚举仅当 $0 \le nj\le k$ 时合法,转移公式为 $dp_{i,nj,ns}= \min (dp_{i,nj,ns},dp_{i-1,j,st}+cost)$。
你可以滚动数组。
## 最后贴上代码:::success[代码]
:::success[AC code]```cpp#include<bits/stdc++.h>#define ll long long#define re register4 行未改动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;}```:::