[CSP-J 2026] 回文串 / string 题解

· · 题解

【CSP-J 2026 T2】回文串 题解

先在这里推荐一下我的Github:Lrz66666666

两个团队:TJ出题组 流浪马戏团OI

非常水的一道T2 (我绝对不会告诉你我比赛的时候打出正解花了我一个半小时)。

初一蒟蒻第一篇题解,不喜勿喷。

题意简述

字符串由 n 段组成,第 i 段包含 k_i 个相同字符 c_i。 我们可以修改任意位置的字符,求把整个串改成回文串的最小修改次数。

暴力

思路

朴实无华:把字符串展开 + 暴力枚举对称位置。

题目要求最少修改次数,如果把样例1,2展开手动模拟 (样例3展开之后都到2027了)。

如图:

发现规律了吧。

(即暴力遍历所有对称位置对,如果一对字符不相等,就给答案加一,cnt为答案)。 ### 正确性证明 回文要求对称位置字符相等。如果不能成对匹配,必须改一个让它们一样,改哪个都可以,不影响结果。 ### 暴力 Code ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,k,p,cnt; string s; char c; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(ll i = 1;i <= n;i++){ cin >> k >> c; for(ll j = 1;j <= k;j++){ s += c; } p += k; } for(ll i = 0;i < p / 2;i++){ if(s[i] != s[p - i - 1]) cnt++; } cout << cnt << "\n"; return 0; } ``` ### 测试点信息 ![](https://cdn.luogu.com.cn/upload/image_hosting/ceeydp4f.webp) ### 问题 时间复杂度$\text{O(L)}$,空间复杂度$\text{O(L)}$。 观察题目数据范围: ![](https://cdn.luogu.com.cn/upload/image_hosting/wmu27m1h.webp) 在 $13 \sim 16$ 和 $22 \sim 25$ 的测试点中,$L$会大于 $2\times 10^{12}$,遍历最少需要 $1\times 10^{12}$ 次,也就是$10000$秒,直接$\text{TLE}$,而且展开之后空间也会爆,导致$\text{MLE}$。 那怎么办呢? ## 正解 ### 思路 让我们再次观察题目: 以样例1举例: ![](https://cdn.luogu.com.cn/upload/image_hosting/p5ibmyuh.webp) 此时,我们会惊奇的发现:答案竟是正确的! 不信?那就继续模拟! 样例输入: $$ \begin{aligned} &3\ \texttt{a}\\ &2\ \texttt{b}\\ &4\ \texttt{c}\\ &1\ \texttt{b}\\ &5\ \texttt{d} \end{aligned} $$ 样例输出: $$\boldsymbol{6}$$ 模拟:($p$为两端的剩余量最小值) 1. $\mathit{p}=\min(3,5)=3$,$\texttt{a}\neq\texttt{d}$,$\mathit{cnt} \gets 3$;段 $1$ 耗尽,$l=2$,段 $5$ 剩余长度为 $2$。 2. $\mathit{p}=\min(2,2)=2$,$\texttt{b}\neq\texttt{d}$,$\mathit{cnt} \gets 5$;段 $2$、段 $5$ 耗尽,$l=3,\ r=4$。 3. $\mathit{p}=\min(4,1)=1$,$\texttt{c}\neq\texttt{b}$,$\mathit{cnt} \gets 6$;段 $4$ 耗尽,$r=3$。 此时,仅剩段$3$,模拟结束,$cnt = 6$(按接下来的解法,其实是$l = r = 3$,左右指针重合才退出,这里方便理解)。 好了,其实思路很简单,仔细分析刚才的模拟过程,便会发现它的实现,就是经典算法——**双指针**。 ### 具体实现 定义 $l = 1,\ r = n,\ p = 0,\ \mathit{cnt} = 0$。(具体含义在之前讲了) 定义结构体数组$a$,包含一个$char$和一个$long long$(不开$long long$见祖宗)。 随后正常输入,最后输出$cnt$即可。 **(不包含算法!!!)** 上述实现如下:~~(学过1秒的都会)~~ ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,l,r,p,cnt; struct akcspj{ char c; ll k; }a[200005]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; l = 1; r = n; for(ll i = 1;i <= n;i++){ cin >> a[i].k >> a[i].c; } //…… cout << cnt; return 0; } ``` **好了,划重点!!!** 整个算法遍历每一段至多一次,时间复杂度$\text{O(n)}$,空间复杂度$\text{O(n)}$。 因为如果$l$碰到$r$,代表肯定是没有可以更改的了,所以条件为$l < r$。 每轮$p$为两端最小值,即: ```cpp p = min(a[l].k,a[r].k); ``` 关键一步:**判断是否同字母!**,如果不同需要 $\mathit{cnt}+p$。 ```cpp if(a[l].c != a[r].c){ cnt += p; } ``` 接着两边抵消掉$p$。 ```cpp a[l].k -= p; a[r].k -= p; ``` 最后哪边已经为$0$了,就跳到下一个。 ```cpp if(a[l].k == 0) l++; if(a[r].k == 0) r--; ``` ### 正确性证明 回文要求对称位置字符相等。设左右指针 $l,r$ 指向当前最外侧未配对的段,取 $p=\min(a[l].k,a[r].k)$,这是两段可配对的字符对数。若字符不同,则这 $p$ 对需要付出 $p$ 的修改代价;相同则无需代价。消耗后将余量为 $0$ 的段向内移动指针,直到 $l=r$。 最外层的对称配对是强制的,该贪心不会破坏最优解,最终累加得到最小修改次数。 思路阐述就这样水灵灵地结束了。 ## 总结(代码) 题目算法实现不难,主要是想到这个挺麻烦 ~~感谢纵享丝滑和厕所的水龙头~~ ,感觉有点搞心态。 最后再把 $\text{AC Code}$和赛时代码敬上。 ### AC Code ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,l,r,p,cnt; struct akcspj{ char c; ll k; }a[200005]; int main(){ //freopen("string.in","r",stdin); //freopen("string.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; l = 1; r = n; for(ll i = 1;i <= n;i++){ cin >> a[i].k >> a[i].c; } while(l < r){ p = min(a[l].k,a[r].k); if(a[l].c != a[r].c){ cnt += p; } a[l].k -= p; a[r].k -= p; if(a[l].k == 0) l++; if(a[r].k == 0) r--; } cout << cnt; return 0; } ``` ![](https://cdn.luogu.com.cn/upload/image_hosting/jvpcyx86.webp) (其实这个是赛后在赛时代码基础上的改良版,虽然赛时代码也是$\text{AC}$,但是有点绕,用了一个$t$,初始是$n$,判断是否大于1,就是刚才第一个模拟的逻辑,放下面了) ### 赛时代码 ```cpp #include<bits/stdc++.h> using namespace std; long long n,t,l,r,cnt; struct rpjj{ long long k; char c; }a[200005]; int main(){ // freopen("string.in","r",stdin); // freopen("string.out","w",stdout); cin >> n; for(long long i = 1;i <= n;i++){ cin >> a[i].k >> a[i].c; } if(n == 1){ cout << 0; return 0; } t = n; l = 1; r = n; while(t > 1){ long long p = a[l].k; while(p >= a[r].k){ if(a[r].c != a[l].c) cnt += a[r].k; p -= a[r].k; r--; t--; } if(p == 0){ l++; }else{ a[r].k -= p; if(a[l].c != a[r].c) cnt += p; l++; } t--; } cout << cnt; return 0; } ``` ![屏幕截图 2026-11-06 213612.png](https://cdn.luogu.com.cn/upload/image_hosting/j1gg4mfj.webp) **完结撒花!** 最后,感谢各位看完整整$300$多行题解,祝各位在未来,永远 **$RP++$**! (记得点赞,评论和关注!谢谢!)