最长交错匹配

yehongchen 2023-12-23 20:02:08 0
#include <bits/stdc++.h>
using namespace std;
char a[1005], b[1005];
int n, m, f[1005][1005];
void dp() {
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int t1 = -10, t2 = -10;
            t2 = max(f[i - 1][j], f[i][j - 1]);
            for(int x=i-1;x>=1;x--)
                for(int y=i-1;y>=1;y--)
                    if(a[x]==b[j]&&a[i]==b[y])
                        t1=max(t1,f[x-1][y-1]);
            t1=t1+2;
			f[i][j]=max(t1,t2);            
        }
    cout << f[n][m] << "\n";
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> b[i];
    dp();
    return 0;
}
{{ vote && vote.total.up }}