题解:CF2205E Simons and Dividing the Rhythm

· · 题解

前言

原来这题没有想象的那么难,但 F 才是真水吧。这题需要一点有关字符串的灵感。

sol

跟区间和计数有关,考虑 DP。定义 f_i 表示能变成 t 的前缀 [1,i] 的不同字符串 s 的个数。由于 n\le 8\times 10^3,允许我们直接枚举区间 [j,i] 进行转移。但是难点在于枚举的每个区间都进行转移的话可能会算重,因为不同的字符串会有多种切割方案。

根据观察和尝试能发现,若一个子串存在 border,那么一定可以对其再进行切割,这启发我们在转移时只转移无 border 的区间。

结论证明

但是这样的正确性仍没那么明显,我们需要证明:任意一个可行的 s 一定存在唯一的方案,使得分割出的每个区间都不存在 border。我们分两步证明:

1.不会存在超过一种方案满足不存在 border。

考虑反证法,设已知存在的一种方案为 A,假设 B 方案也满足不存在 border。对比 A 和 B,找到两种方案第一个不同的区间 [l,r1] 和 [l,r2],满足 r1\ne r2。通过逐位对应可知,这两个区间中较长的那个一定存在 border,与假设都不存在 border 相矛盾。

2.一定存在至少一种方案满足不存在 border。

考虑构造法,由于 s 可行,那么一定存在一种切割方案。修改这个切割方案,我们从前往后,不断找到第一个存在 border 的区间,并将它切开,直到不能切位置,就能得到一个不存在 border 的方案。

综上:如果 s 可行,那么切割方案唯一。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
#include <cstring>
#include <set>
#include <ctime>
#include <cstdlib>
#define int long long
using namespace std;
int read()
{
    char c=getchar();
    int f=1,x=0;
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=(x<<1)+(x<<3)+(c^'0');
        c=getchar();
    }
    return x*f;
}
void print(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
const int N=8e3+5,mod=998244353;
int T,n,a[N],g[N][N],f[N];
signed main()
{
    T=read();
    while(T--)
    {
        n=read();
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=1;i<=n;i++)
        {
            g[i][i]=0;
            for(int j=i+1;j<=n;j++)
            {
                int now=g[i][j-1];
                while(now&&a[now+1]!=a[j]) now=g[i][now];
                if(now) g[i][j]=now+1;
                else g[i][j]=(a[i]==a[j]?i:0);
            }
        }
        f[0]=1;
        for(int i=1;i<=n;i++)
        {
            f[i]=0;
            for(int j=1;j<=i;j++)
            {
                if(!g[j][i])
                {
                    f[i]+=f[j-1];
                    if(f[i]>=mod) f[i]-=mod;
                }
            }
        }
        print(f[n]);
        putchar('\n');
    }
    return 0;
}
/*
1
10
2 3 1 4 3 1 4 3 1 2
*/