题意:求管道输出的全部可能情况的个数的平方求和,有点绕
比如样例:
AB
B
输出:BAB 1种 BBA 2种,所以输出5
题解:第一次知道可以把求平方,转化为两个人玩游戏.......
两个人在两个独立的装置里取球,输出队列相同的方案数
因为对于每一种输出队列,第一个人有 种方案,第二个人有
种方案,那么两个人输出队列相同的方案总数就是
表示第一次取A串中的前i个,取B串中的前j个,第二次取A串中的前k个,B串中的前l个的方案数,并且
然后就可以写出dp式子
然后因为 可以压缩一维
然后这样还是会超,可以把第一维再次压缩
#include<bits/stdc++.h>
using namespace std;
const int mod = 1024523;
const int maxn = 505;
int dp[2][maxn][maxn];
int n, m;
char str1[maxn], str2[maxn];
int main(void)
{
while(cin >> n >> m)
{
scanf(" %s %s", str1, str2);
for(int i = 0; i < n/2; i++) swap(str1[i], str1[n-i-1]);
for(int i = 0; i < m/2; i++) swap(str2[i], str2[m-i-1]);
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
int cur = 0, pre;
for(int i = 0; i < n+m; i++)
{
pre = cur;
cur = !pre;
memset(dp[cur], 0, sizeof(dp[cur]));
int l = max(i-m, 0);
int r = min(i, n);
for(int up1 = l; up1 <= r; up1++)
{
for(int up2 = l; up2 <= r; up2++)
{
if(!dp[pre][up1][up2]) continue;
int down1 = i-up1;
int down2 = i-up2;
//都取上
if(str1[up1] == str1[up2]) dp[cur][up1+1][up2+1] = (dp[cur][up1+1][up2+1]+dp[pre][up1][up2])%mod;
//p1上p2下
if(str1[up1] == str2[down2]) dp[cur][up1+1][up2] = (dp[cur][up1+1][up2]+dp[pre][up1][up2])%mod;
//都取下
if(str2[down1] == str2[down2]) dp[cur][up1][up2] = (dp[cur][up1][up2]+dp[pre][up1][up2])%mod;
//p1下p2上
if(str2[down1] == str1[up2]) dp[cur][up1][up2+1] = (dp[cur][up1][up2+1]+dp[pre][up1][up2])%mod;
}
}
}
printf("%d\n", dp[cur][n][n]);
}
return 0;
}
//code:https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40386493
京公网安备 11010502036488号