跳到正文

题解

[FJOI2016]所有公共子序列问题-解题报告

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

文章信息

发布
2019年5月8日
收录
2026年5月11日
更新
2026年11月13日
篇幅
约 6 分钟
题目
P4608

版本

  1. v1当前

n,m不要打反

不错,自己搞出来了

看题解都是无法滚动数组的压位高精,给一个可以滚动数组的DP。

首先,这道题有一个很显然的二维dp[i][j]dp[i][j]表示恰好选择了i和j:

dp[i][j]=1+∑wdp[nxt[i][w]][nxt′[j][w]];a[i]=b[j]dp[i][j]=1+\sum _w dp[nxt[i][w]][nxt'[j][w]];a[i]=b[j]

但是加上高精度之后,这道题会炸空间,怎么办?(压位

我们改进一下状态,dp[i][j]dp[i][j]表示第二个串恰好选了j,第一个串选了≤i\le i的位置。

那么转移有两种,我们还是必须贪心地匹配,才能保证只计数一次:

  1. 如果a[i]=b[j]a[i]=b[j]:
dp[i][j]=1+∑wdp[i−1][nxt′[j][w]]dp[i][j]=1+\sum_w dp[i-1][nxt'[j][w]]

枚举下一个是什么。

  1. 否则:
dp[i][j]=dp[i−1][j]dp[i][j]=dp[i-1][j]

忽略掉i即可。

为什么这个做法更优秀呢?因为这个做法可以扩展到LCIS计数且字符集很大且卡空间的情况。

由于没有压位导致常数很大的代码:

C++
#define N 3015
il int ha(const char c)
{
	if(c<='Z') return c-'A';
	return c-'a'+26;
}
struct XL
{
	char s[N];
	int n,nxt[N][52];
	void init()
	{
		scanf("%s",s+1); n=strlen(s+1);
		for(ri i=n-1; i>=0; --i)
		{
			memcpy(nxt[i],nxt[i+1],sizeof nxt[i]);
			nxt[i][ha(s[i+1])]=i+1;
		}
	}
} A,B;
int typ;
int cnt;
int d[N],dep;
void dfs(int x,int y)
{
	if(typ)
	{
		for(ri i=1; i<=dep; ++i) putchar(d[i]<26?d[i]+'A':d[i]-26+'a');
		enter;
	}
	++cnt;
	for(ri i=0; i<52; ++i) if(A.nxt[x][i]&&B.nxt[y][i])
		{
			d[++dep]=i;
			dfs(A.nxt[x][i],B.nxt[y][i]);
			--dep;
		}
}
#define MXLEN 200
struct Bigint
{
	int num[MXLEN],sz;
	Bigint(int x = 0)
	{
		mem(num,0),sz=0;
		while(x) num[++sz]=x%10,x/=10;
	}
	int &operator[](const int x) {return num[x];}
	const int &operator[](const int x) const {return num[x];}
	void operator*=(const Bigint &x)
	{
		static int tmp[MXLEN]; clr(tmp+1,tmp+1+sz+x.sz);
		for (ri i=1; i<=sz; ++i)
			for(ri j=1; j<=x.sz; ++j) tmp[i+j-1]+=num[i]*x[j];
		sz+=x.sz;
		for(ri i=1; i<=sz; ++i)
		{
			tmp[i+1]+=tmp[i]/10;
			tmp[i]%=10;
		}
		while(sz&&tmp[sz]==0) --sz;
		for(ri i=1; i<=sz; ++i) num[i]=tmp[i];
	}
	Bigint operator*(const Bigint &x) const
	{
		Bigint res=*this; res*=x;
		return res;
	}
	void operator*=(const int x)  // positive
	{
		for(ri i=1; i<=sz; ++i) num[i]*=x;
		sz+=10;
		for(ri i=1; i<=sz; ++i)
		{
			num[i+1]+=num[i]/10;
			num[i]%=10;
		}
		while(sz&&num[sz]==0) --sz;
	}
	Bigint operator*(const int x) const
	{
		Bigint res=*this; res*=x;
		return res;
	}
	void operator+=(const Bigint &x)
	{
		for (ri i=1,t; i<=x.sz; ++i)
		{
			t=num[i]+x[i];
			num[i]=t%10;
			num[i+1]+=t/10;
		}
		sz=max(sz,x.sz); while(num[sz+1]) ++sz;
	}
	Bigint operator+(const Bigint &x) const
	{
		Bigint res=*this; res+=x;
		return res;
	}
	void read()
	{
		static char tc[MXLEN];
		scanf("%s", tc+1); sz=strlen(tc+1);
		for(ri i=1; i<=sz; ++i) num[i]=tc[sz-i+1]-'0';
	}
	void print() const
	{
		int t=max(sz,1);
		for(ri i=t; i>=1; --i) putchar('0'+num[i]); enter;
	}
};
int n,m;
Bigint _dp[2][N],ans;
void solve()
{
	auto f=_dp[0],g=_dp[1];
	for(ri i=n; i>=1; --i)
	{
		swap(f,g); memset(f,0,sizeof _dp[0]);
		for(ri j=m; j>=1; --j)
			if(A.s[i]==B.s[j])
			{
				f[j]+=1;
				for(ri k=0; k<52; ++k)
					if(B.nxt[j][k]) f[j]+=g[B.nxt[j][k]];
			}
			else f[j]=g[j];
	}
	ans+=1;
	for(ri k=0; k<52; ++k)
		if(B.nxt[0][k]) ans+=f[B.nxt[0][k]];
	ans.print();
}
signed main()
{
#ifdef M207
	freopen("in.in","r",stdin);
	// freopen("ot.out","w",stdout);
#endif
	in(n,m);
	A.init(),B.init();
	in(typ);
	if(typ) {dfs(0,0); out(cnt);}
	else solve();
	return 0;
}
原文
存档评论1 条抓取于