1 条题解

  • 0
    @ 2025-4-11 16:24:43
    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 500;
    const int MOD = 1024523;
    int n, m;
    string s1, s2;
    // f[a滚动][b][x][y计算]
    // 第一个人上面拿 a 个下面拿 b 个
    // 第二个人上面拿 x 个下面拿 y 个
    // 的方案数
    int f[2][MAXN + 5][MAXN + 5];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cin >> n >> m;
    	cin >> s1;
    	cin >> s2;
    	int U = s1.size(), D = s2.size();
    	for (int a = 0; a <= U; a++)
    	{
    		for (int b = 0; b <= D; b++)
    			for (int x = 0; x <= U; x++)
    			{
    				int y = a + b - x;
    				if (y < 0 || D < y)
    					continue;
    				if (a == 0 && b == 0 && x == 0 && y == 0)
    				{
    					f[a % 2][b][x] = 1;
    					continue;
    				}
    				f[a % 2][b][x] = 0;
    				if (a && x && s1[U - a] == s1[U - x]) // 上上
    					f[a % 2][b][x] += f[1 - a % 2][b][x - 1];
    				if (a && y && s1[U - a] == s2[D - y]) // 上下
    					f[a % 2][b][x] += f[1 - a % 2][b][x];
    				if (b && x && s2[D - b] == s1[U - x]) // 下上
    					f[a % 2][b][x] += f[a % 2][b - 1][x - 1];
    				if (b && y && s2[D - b] == s2[D - y]) // 下下
    					f[a % 2][b][x] += f[a % 2][b - 1][x];
    				f[a % 2][b][x] %= MOD;
    				// cout << a << " " << b << " " << x << " " << y << ":" << f[a % 2][b][x] << "\n";
    			}
    		for (int b = 0; b <= D; b++)
    			for (int x = 0; x <= U; x++)
    				f[1 - a % 2][b][x] = 0;
    	}
    	cout << f[U % 2][D][U];
    	return 0;
    }
    
    • 1

    信息

    ID
    13399
    时间
    2000ms
    内存
    125MiB
    难度
    7
    标签
    递交数
    21
    已通过
    8
    上传者