跳到正文

题解

题解:CF2276E Good Substrings

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

文章信息

发布
2026年10月16日
收录
2026年10月16日
更新
2026年11月16日
篇幅
约 7 分钟
获赞
17
题目
CF2276E

版本

  1. v3当前
  2. v2
  3. v1

CF2276E Good Substrings 题解

原题传送门

题意

给定一个长度为 nn 的数字字符串 ss。定义一个子串 sl…rs_{l \dots r} 是坏的,当且仅当它包含 2025 作为子序列,但不包含 2026 作为子序列。否则子串是好的。

对于每个位置 ii(1≤i≤n1 \le i \le n),计算包含该位置的好子串数量。

Solution

对于固定的左端点 ll,随着右端点 rr 向右移动,子串包含的子序列集合是单调递增的。因此存在两个临界位置:

  • point1point_{1}:最小的 rr,使得 sl…rs_{l \dots r} 包含 2025 作为子序列。若不存在,令 point1=n+1point_{1} = n+1。
  • point2point_{2}:最小的 rr,使得 sl…rs_{l \dots r} 包含 2026 作为子序列。若不存在,令 point2=n+1point_{2} = n+1。

那么对于以 ll 开头的子串:

  • 若 r<point1r < point_{1}:不包含 2025,是好的。
  • 若 point1≤r<point2point_{1} \le r < point_{2}:包含 2025 但不包含 2026,是坏的。
  • 若 r≥point2r \ge point_{2}:包含 2026,是好的。

因此:

  • 当 point1≥point2point_{1} \ge point_{2} 时,不存在坏子串,所有子串都是好的。
  • 当 point1<point2point_{1} < point_{2} 时,坏子串的右端点范围为 [point1,point2−1][ point_{1}, point_{2}-1 ]。

因此我们需要对每个位置 ii 统计覆盖它的好子串数量。

考虑每个左端点 ll 对答案的贡献:

  1. 默认贡献:假设所有子串都是好的,那么对于左端点 ll,右端点从 ll 到 nn。对于位置 i≥li \ge l,覆盖它的子串数量为 n−i+1n - i + 1。
    这可以看作在区间 [l,n][l, n] 加上 (n+1)−i(n+1) - i。

  2. 修正坏子串:如果 point1<point2point_{1} < point_{2},则坏子串的右端点在 [point1,point2−1][point_{1}, point_{2}-1]。这些坏子串对位置 ii 的覆盖数量为:

    • 若 i<point1i < point_{1}:所有坏子串都覆盖 ii,数量为 point2−point1point_{2} - point_{1}。
    • 若 point1≤i≤point2−1point_{1} \le i \le point_{2}-1:覆盖 ii 的坏子串数量为 point2−ipoint_{2} - i。
    • 若 i≥point2i \ge point_{2}:没有坏子串覆盖 ii,数量为 00。

    因此,我们需要从默认贡献中减去这个分段函数。

我们可以使用两个差分数组 cfk 和 cfm 来维护区间上的线性函数 k⋅i+mk \cdot i + m。
对于每个左端点 ll,执行以下更新:

  • 默认贡献:在 [l,n][l, n] 上加上 k=−1,m=n+1k = -1, m = n+1。
  • 若 point1<point2point_{1} < point_{2}:
    • 在 [l,point1−1][l, point_{1}-1] 上减去常数 point2−point1point_{2} - point_{1}(即加上 k=0,m=−(point2−point1)k=0, m=-(point_{2}-point_{1}))。
    • 在 [point1,point2−1][point_{1}, point_{2}-1] 上减去 point2−ipoint_{2} - i(即加上 k=1,m=−point2k=1, m=-point_{2})。

最后对差分数组求前缀和,即可得到每个位置 ii 的答案。

预处理 nxt 数组

为了快速计算每个左端点 ll 的 point1point_{1} 和 point2point_{2},我们预处理 nxt[i][d]:
表示从位置 ii 开始(包括 ii)数字 dd 第一次出现的位置,若不存在则为 n+1n+1。
从右向左遍历字符串,维护 cur[d] 为当前后缀中数字 dd 最近出现的位置,初始为 n+1n+1。

然后对于每个 ll,贪心地查找 2025 和 2026 的匹配位置,每次跳转到 nxt[poi][digit]。由于模式串长度均为 4,每个 ll 最多查找 4 步。

复杂度分析

  • 预处理 nxt:O(10n)O(10n)
  • 计算所有 ll 的 point1,point2point_{1}, point_{2}:O(4n)O(4n)
  • 差分更新:每个 ll 常数次操作,O(n)O(n)
  • 前缀和还原答案:O(n)O(n)

总时间复杂度 O(n)O(n),空间复杂度 O(10n)O(10n)。

AC code

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int s2025[5]={0,2,0,2,5},s2026[5]={0,2,0,2,6};
int n;
string s;
int xxkan[300005][10]; //nxt数组
int qwq[10];
int cf1[300005]; //即k的差分
int cf2[300005]; //即m的差分

signed main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>s;
        s=" "+s;
        memset(qwq,0x3f,sizeof(qwq));       //多测不清空,亲人两行泪
        for(int i=0;i<=n+2;i++) cf1[i]=cf2[i]=0;
        for(int i=n;i>=1;i--)        //预处理nxt数组
        {
            qwq[s[i]-'0']=i;
            for(int j=0;j<=9;j++)
            {
                xxkan[i][j]=qwq[j];
            }
        }
        for(int i=1;i<=n;i++)
        {
            int poi1=i,poi2=i,xx1=0,xx2=0;
            while(xx1<4&&poi1<=n) poi1=xxkan[poi1][s2025[++xx1]]; //找到point1
            while(xx2<4&&poi2<=n) poi2=xxkan[poi2][s2026[++xx2]]; //找到point2
            if(poi1>n) poi1=n+1; //找不到
            if(poi2>n) poi2=n+1;
            cf1[i]--;        //假设全都是好的
            cf1[n+1]++;
            cf2[i]+=n+1;
            cf2[n+1]-=n+1;
            if(poi1<poi2)      //如果存在不好的序列在区间内
            {
                cf2[i]-=poi2-poi1;          //将不好的序列删掉
                cf2[poi1]+=poi2-poi1;
                cf1[poi1]++;
                cf1[poi2]--;
                cf2[poi1]-=poi2;
                cf2[poi2]+=poi2;
            }
        }
        int cnt1=0,cnt2=0;
        for(int i=1;i<=n;i++)
        {
            cnt1+=cf1[i];
            cnt2+=cf2[i];
            cout<<cnt1*i+cnt2<<" ";   //对差分求前缀和,输出
        }
        cout<<"\n";
    }
    return 0;
}

本文使用 deepseek 进行了 latex 修正与格式调整。

原文
存档评论17 条抓取于