跳到正文

题解

题解:P14230 不连续子串 / subseq

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

文章信息

发布
2026年3月12日
收录
2026年3月13日
更新
2026年11月13日
篇幅
约 7 分钟
题目
P14230

版本

  1. v1当前

对于本质不同子序列,我们通常在所有值相同的子序列中钦定最靠前的那个来计数,其余的直接视为不合法的子序列。比如序列 a=[1,2,2,3,3]a=[1,2,2,3,3] 中,下标集合 {1,2,4},{1,2,5},{1,3,4},{1,3,5}\{1,2,4\},\{1,2,5\},\{1,3,4\},\{1,3,5\} 都对应子序列 [1,2,3][1,2,3],但只有下标集合 {1,2,4}\{1,2,4\} 会被统计,因为它是最靠前的。

考虑怎么判断一个子序列是不是同种子序列中最靠前的。设序列 aa 中一个子序列的下标集合为 b1,b2,…,bm{b_1,b_2,\dots,b_m},再令 b0=0b_0=0,则这个子序列是最靠前的当且仅当 ∀1≤i≤m,bi−1<j<bi\forall 1\le i\le m,b_{i-1}<j<b_i,aj≠abia_j\neq a_{b_i},即对于子序列中的每个数 xx ,它与它左侧的那个数之间不能有等于 xx 的数,否则可以用那个数替换 xx 使得子序列更靠前。

定义一些记号:

  • C(b)C(b) 表示 bb 的本质不同子序列的数量(包含空序列,下同);
  • M(b)M(b) 表示 bb 的本质不同子序列的本质不同子序列的数量;
  • S(b)S(b) 表示 bb 的所有本质不同子序列构成的集合
  • ci=C(a1∼i)c_i=C(a_{1\sim i}),mi=M(a1∼i)m_i=M(a_{1\sim i}),si=S(a1∼i)s_i=S(a_{1\sim i});
  • dp(b,x)dp(b,x) 表示 bb 的所有本质不同子序列中,结尾的右侧还有等于 xx 的数的子序列数量。
  • f(s,x)=∑b∈S(s)dp(b,x)f(s,x)=\sum_{b\in S(s)}dp(b,x);
  • end(b)end(b) 表示 bb 最后(最右)的元素;
  • lstilst_i 表示 aia_i 上一次出现的位置。

题目的答案为 ∑b∈S(a)(C(b)−1)=(∑b∈S(a)C(b))−C(a)=mn−cn\sum_{b\in S(a)}(C(b)-1)=(\sum_{b\in S(a)}C(b))-C(a)=m_n-c_n。

考虑 m,cm,c 怎么转移,我们需要对每个 aia_i 求出以 aia_i 结尾的所有本质不同子序列的数量(记作 rcirc_i)和 CC 值之和(记作 rmirm_i),那么

mi=mi−1+rmici=ci−1+rci\begin{aligned} m_i&=m_{i-1}+rm_i\\ c_i&=c_{i-1}+rc_i \end{aligned}

考虑 aia_i 可以接在哪些子序列 bb 的后面。由上文提到的判断子序列是不是最靠前的方法,[end(b),i−1][end(b),i-1] 之间不能有等于 aia_i 的数,即 lsti≤end(b)<ilst_i\le end(b)<i。

现在我们知道了子序列 bb 后面能接 aia_i 的条件,rcirc_i 的计算就是简单的了:

rci=∣{b+ai∣lsti≤end(b)<i,b∈si−1}∣=∣{b∣end(b)≤i−1,b∈si−1}∣−∣{b∣end(b)≤lsti−1,b∈si−1}∣=∣si−1∣−∣slsti−1∣=ci−1−clsti−1\begin{aligned} rc_i=&|\{b+a_i\mid lst_i\le end(b)<i,b\in s_{i-1}\}|\\ =&|\{b\mid end(b)\le i-1,b\in s_{i-1}\}|-|\{b\mid end(b)\le lst_i-1,b\in s_{i-1}\}|\\ =&|s_{i-1}|-|s_{lst_i-1}|\\ =&c_{i-1}-c_{lst_i-1} \end{aligned}

现在计算 rmirm_i。考虑如何计算 C(b+ai)C(b+a_i),我们分为两种:

  • 不包含 aia_i:子序列数量为 C(b)C(b)
  • 包含 aia_i:子序列数量为 C(b)−dp(b,ai)C(b)-dp(b,a_i),因为 bb 中的一些子序列的结尾和 aia_i 之间有等于 aia_i 的数,这些序列不能被统计。

所以我们得到 CC 的递推式:

C(b+ai)=2C(b)−dp(b,ai)C(b+a_i)=2C(b)-dp(b,a_i)

然后得到:

rmi=∑b∈si−1∧lsti≤end(b)<iC(b+ai)=2∑b∈si−1∧lsti≤end(b)<iC(b)−∑b∈si−1∧lsti≤end(b)<idp(b,ai)=2(∑b∈si−1C(b)−∑b∈slsti−1C(b))−(∑b∈si−1dp(b,ai)−∑b∈slsti−1dp(b,ai))=2(mi−1−mlsti−1)−(f(i−1,ai)−f(lsti−1,ai))\begin{aligned} rm_i&=\sum_{b\in s_{i-1}\wedge lst_i\le end(b)<i}C(b+a_i)\\ &=2\sum_{b\in s_{i-1}\wedge lst_i\le end(b)<i}C(b)-\sum_{b\in s_{i-1}\wedge lst_i\le end(b)<i}dp(b,a_i)\\ &=2\left(\sum_{b\in s_{i-1}}C(b)-\sum_{b\in s_{lst_i-1}}C(b)\right)-\left(\sum_{b\in s_{i-1}}dp(b,a_i)-\sum_{b\in s_{lst_i-1}}dp(b,a_i)\right)\\ &=2(m_{i-1}-m_{lst_i-1})-(f(i-1,a_i)-f(lst_i-1,a_i))\\ \end{aligned}

rmirm_i 的计算依赖 ff,所以我们再想一想 f(i,x)f(i,x) 的转移。分为两类:

  • x=aix=a_i:
f(i,x)=∑b∈sidp(b,x)=∑b∈si−1dp(b,x)+∑b∈si∧end(b)=idp(b,x)=f(i−1,x)+∑b∈si−1∧lsti≤end(b)<iC(b)=f(i−1,x)+∑b∈si−1C(b)−∑b∈slsti−1C(b)=f(i−1,x)+mi−1−mlsti−1\begin{aligned} f(i,x)&=\sum_{b\in s_i}dp(b,x)\\ &=\sum_{b\in s_{i-1}}dp(b,x)+\sum_{b\in s_i\wedge end(b)=i}dp(b,x)\\ &=f(i-1,x)+\sum_{b\in s_{i-1}\wedge lst_i\le end(b)<i}C(b)\\ &=f(i-1,x)+\sum_{b\in s_{i-1}}C(b)-\sum_{b\in s_{lst_i-1}}C(b)\\ &=f(i-1,x)+m_{i-1}-m_{lst_i-1} \end{aligned}
  • x≠aix\neq a_i:
f(i,x)=∑b∈sidp(b,x)=∑b∈si−1dp(b,x)+∑b∈si∧end(b)=idp(b,x)=f(i−1,x)+∑b∈si−1∧lsti≤end(b)<idp(b,x)=f(i−1,x)+∑b∈si−1dp(b,x)−∑b∈slsti−1dp(b,x)=2f(i−1,x)−f(lsti−1,x)\begin{aligned} f(i,x)&=\sum_{b\in s_i}dp(b,x)\\ &=\sum_{b\in s_{i-1}}dp(b,x)+\sum_{b\in s_i\wedge end(b)=i}dp(b,x)\\ &=f(i-1,x)+\sum_{b\in s_{i-1}\wedge lst_i\le end(b)<i}dp(b,x)\\ &=f(i-1,x)+\sum_{b\in s_{i-1}}dp(b,x)-\sum_{b\in s_{lst_i-1}}dp(b,x)\\ &=2f(i-1,x)-f(lst_i-1,x) \end{aligned}

综上这道题就做完了。时空复杂度 O(n2)O(n^2)。

C++
#include<bits/stdc++.h>
#define N 8005
#define mod 1000000007
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return x*f;
}
int n,a[N];
int lst[N],M[N],C[N],f[N][N];
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    M[0]=C[0]=1;
    for(int i=1;i<=n;i++)
    {
        int x=a[i],L=lst[x];
        int rc=(C[i-1]-(L>0?C[L-1]:0)+mod)%mod;
        int rm=(2ll*(M[i-1]-(L>0?M[L-1]:0)+mod)%mod-(f[i-1][x]-(L>0?f[L-1][x]:0)+mod)%mod+mod)%mod;
        M[i]=(M[i-1]+rm)%mod;
        C[i]=(C[i-1]+rc)%mod;
        for(int v=1;v<=n;v++) if(v!=x) f[i][v]=(2ll*f[i-1][v]-(L>0?f[L-1][v]:0)+mod)%mod;
        f[i][x]=(f[i-1][x]+(M[i-1]-(L>0?M[L-1]:0)+mod)%mod)%mod;
        lst[x]=i;
    }
    printf("%d\n",(M[n]-C[n]+mod)%mod);
    return 0;
}
原文