ABC299F-Square Subsequence-DP,子序列方案数
ABC299F - Square Subsequence
- https://www.luogu.com.cn/problem/AT_abc299_f
- DP、子序列方案数
题意
给出一个长度为
- 子序列为
TT 的形式,代表由两个T 拼接而成。其中T 是S 的一个子序列。
思路
hint 1
考虑一个简化版本的问题:
给出一个字符串,求它的本质不同的子序列的数量。
- 例题:https://leetcode.cn/problems/distinct-subsequences-ii/description/
例如,给出字符串
因此,我们可以令
转移:
其中
for (int i=0; i<26; i++)
f[ nxt[1][i] ] = 1;
for(int i=1; i<=n; i++)
for(int j=0; j<26; j++)
f[ next[i][j] ] += f[i];
for (int i=1; i<=n; i++)
ans += dp[i];
hint 2
注意到子序列为
不难想到可以枚举第一个子序列末尾的字符所选的位置,并且第二个子序列的开头字母要在它后面。
转移有:
for(int i=1; i<=n; i++)
{
memset(dp, 0, sizeof(dp));
dp[0][i] = 1;
for(int j=0; j<=i; j++)
for(int k=i; k<=n; k++)
for(int c=0; c<26; c++)
if(next[j][c]>=1 && next[j][c]<=i && next[k][c]>=1 && next[k][c]<=n)
dp[next[j][c]][next[k][c]] += dp[j][k];
for(int j=i;j<=n;j++)
ans += dp[i][j];
}