【hihocoder 1554】最短的 Nore0061

【链接】http://hihocoder.com/problemset/problem/1554


【题意】


中文题

【题解】


DP;
设f[i][j][k]表示前i个字符,第一个串已经得到了前j个字符,第二个串已经得到了前k个字符的最少需要字符串长度.
如果想坚持f[i-1][j][k]的[j][k]状态的话,就必须在第i个字符继续坚持,也即再加上一个字符.
或者你想重新开始,所以每次新的f[i]中f[i][0][0] = 0,其余f[i]=INF;
或者可以和第一个字符串匹配到,则从f[i-1][j][k]变为f[i][jj+1][k],或者是f[i][j][k+1]...

【错的次数】


3

【反思】


一开始写的二分+贪心。。然而完全是瞎猜的。

【代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)


typedef pair<int,int> pii;
typedef pair<LL,LL> pll;


const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N1 = 100;
const int N2 = 3000;
const int INF = 0x3f3f3f3f;


char a[N1+10],b[N1+10],s[N2+10];
int lena,lenb,f[2][N1+10][N1+10];


int main(){
    //Open();
    //Close();
    rs(a);
    rs(b);
    rs(s);
    lena = strlen(a+1),lenb = strlen(b+1);
    int n = strlen(s+1);
    ms(f[0],INF);
    f[0][0][0] = 0;
    int ans = -1;
    rep1(i,1,n){
        int now = i&1,pre = now^1;
        ms(f[now],INF);
        f[now][0][0] = 0;
        rep1(j,0,lena)
            rep1(k,0,lenb)
                if (f[pre][j][k]<INF){
                    f[now][j][k] = min(f[pre][j][k] + 1,
                                       f[now][j][k]);
                    if (s[i] == a[j+1]){
                        f[now][j+1][k] = min(f[now][j+1][k],
                                            f[pre][j][k]+1);
                    }
                    if (s[i] == b[k+1]){
                        f[now][j][k+1] = min(f[now][j][k+1],
                                            f[pre][j][k]+1);
                    }
                }
        int tans = f[now][lena][lenb];
        if (tans >= INF) continue;
        if (ans==-1 || tans < ans){
            ans = tans;
        }
    }
    oi(ans);puts("");
    return 0;
}


原文地址:https://www.cnblogs.com/AWCXV/p/7626106.html