3WA求调
查看原帖
3WA求调
545529
Natori楼主2022/2/2 14:02
#include<iostream>
using namespace std;
const int mod=1024523,N=501,M=501;
char a[N],b[M];
int n,m;
int f[2][M][N],_,tmp;
void modplus(int a,int &b) {
    b+=a;
    if(b>mod)b-=mod;
    return;
}
int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    cin>>(a+1);
    cin>>(b+1);
    f[0][0][0]=1;
    for (int i=0; i<=n; i++,_^=1) {
        for (int j=0; j<=m; j++) {
            for (int k=0; k<=n; k++) {
                tmp=f[_][j][k];
                if (i+j-k<0||i+j-k>m) continue;//越界退出
                //状态转移
                if (a[i+1]==a[k+1]) modplus(tmp,f[_^1][j][k+1]);
                if (b[j+1]==b[i+j-k+1]) modplus(tmp,f[_][j+1][k]);
                if (a[i+1]==b[i+j-k+1]) modplus(tmp,f[_^1][j][k]);
                if (b[j+1]==a[k+1]) modplus(tmp,f[_][j+1][k+1]);
                f[_][j][k]=0;//清除
            }
        }
    }
    cout<<f[_][m][n];
    return 0;
}
2022/2/2 14:02
加载中...