跳到正文

题解

题解:CF2205E Simons and Dividing the Rhythm

fan_xiaoyi约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年10月13日
收录
2026年10月13日
更新
2026年11月13日
篇幅
约 6 分钟
题目
CF2205E

版本

  1. v1当前

CF2205E Simons and Dividing the Rhythm

思路

容易发现若可以通过操作使得 SS 变换为 TT,则可以通过同样的区间选择使得 TT 变换为 SS。
那么题目就可以转化为求 TT 能转化出的 SS 数量。
若没有相同的位,则数量就是 2∣T∣−12^{|T|-1},因为多出一位的本质是可以将其随上一位操作或不随上一位操作,这样可以产生双倍的不同字符串。
转移也是好写的:fi=∑j=0n−1fjf_i=\sum_{j=0}^{n-1} f_j。
考虑何时不能从 jj 转移到 ii:当且仅当区间 [j+1,i][j+1,i] 存在 border。
充分性:若存在 border,那么这个区间可以被表示为 ABAABA 这样的形式,容易发现 [A],[B],[A][A],[B],[A] 的划分与 [ABA][ABA] 的划分是等价的,所以会存在重复,我们只记不存在 border 的那一次。
必要性:若不存在 border,那么一定不会有另一种方法把这段区间换成同样的反转区间,若可以,一定存在 border。

到了这里,后面就比较好处理了,只需要在转移前判断 [i,j][i,j] 是否存在 border 即可,定义为 gi,jg_{i,j}。
定义 lcpi,jlcp_{i,j} 表示从 i,ji,j 开始往后的最长匹配串,转移是容易的,倒序枚举 i,ji,j,若匹配则 lcpi+1,j+1+1→lcpi,jlcp_{i+1,j+1}+1\to lcp_{i,j},否则 0→lcpi,j0\to lcp_{i,j}。
然后差分维护一下哪些位置存在 border,再按照上面的东西 dp 即可。

代码

注意空间问题。

C++
#include<bits/stdc++.h>
#define fir first
#define sec second
//#define int long long
#define pii pair<int,int>
#define double long double
#define tomin(a,b) a=min(a,b)
#define tomax(a,b) a=max(a,b)
#define fep(i,s,e) for(int i=s;i<e;i++)
#define pef(i,s,e) for(int i=s;i>e;i--)
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define per(i,s,e) for(int i=s;i>=e;i--)
namespace FastIO{
	template<typename T>inline void read(T &x){
		x=0;int f=1;char c=getchar();
		for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
		for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
	}
	inline void read(char &c){
        c=getchar();
        while(isspace(c))c=getchar();
    }
    inline void read(std::string &s){
        s.clear();char c=getchar();
		for(;isspace(c);c=getchar());
		for(;!isspace(c)&&c!=EOF;c=getchar())s+=c;
    }
	template<typename T,typename...Args>
    void read(T &x,Args&...args){
        read(x);read(args...);
    }
	template<typename T>void print(T x){
		if(x<0)x=-x,putchar('-');
		if(x>9)print(x/10);
		putchar((x%10)^48);
	}
	inline void print(char c){
		putchar(c);
	}
	inline void print(std::string s){
		fep(i,0,s.size())putchar(s[i]);
	}
	inline void print(const char*s){
    	while(*s)putchar(*s++);
	}
	template<typename T,typename...Args>
    void print(const T &x,const Args&...args){
        print(x);print(args...);
    }
}
using namespace std;
using namespace FastIO;
const int N=8008;
const int mod=998244353;
int T,n,a[N],f[N],hav[N],lcp[N][N];
bitset<N>g[N];
signed main(){
	read(T);
	while(T--){
		read(n);
		rep(i,1,n)read(a[i]),f[i]=0;
		rep(l,1,n)rep(r,1,n)lcp[l][r]=0;
		rep(l,1,n)rep(r,1,n)g[l][r]=0;
		per(i,n,1){
			per(j,n,1){
				if(a[i]==a[j])lcp[i][j]=lcp[i+1][j+1]+1;
				else lcp[i][j]=0;
			}
		}
		rep(l,1,n){
			rep(r,1,n+1)hav[r]=0;
			rep(j,l+1,n){
				int len=lcp[l][j];
				int maxl=min(len,j-l);
				if(maxl>=1){
					hav[j]++;
					hav[j+maxl]--;
				}
			}
			int cur=0;
			rep(r,1,n){
				cur+=hav[r];
				if(cur>0&&r>=l)g[l][r]=1;
			}
		}
		f[0]=1;
		rep(i,1,n){
			rep(j,1,i){
				if(!g[j][i])(f[i]+=f[j-1])%=mod;
			}
		}
		print(f[n],'\n');
	}
}
原文