結果

問題 No.422 文字列変更 (Hard)
ユーザー mamekinmamekin
提出日時 2016-10-05 14:09:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 3,000 ms
コード長 2,810 bytes
コンパイル時間 1,288 ms
コンパイル使用メモリ 120,384 KB
実行使用メモリ 87,936 KB
最終ジャッジ日時 2023-08-13 23:14:38
合計ジャッジ時間 3,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 92 ms
60,948 KB
testcase_02 AC 101 ms
67,532 KB
testcase_03 AC 82 ms
55,808 KB
testcase_04 AC 89 ms
60,912 KB
testcase_05 AC 104 ms
70,516 KB
testcase_06 AC 111 ms
87,936 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 108 ms
80,092 KB
testcase_10 AC 118 ms
85,556 KB
testcase_11 AC 94 ms
61,876 KB
testcase_12 AC 81 ms
55,232 KB
testcase_13 AC 86 ms
57,648 KB
testcase_14 AC 87 ms
59,164 KB
testcase_15 AC 88 ms
59,856 KB
testcase_16 AC 91 ms
61,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int INF = INT_MAX / 2;

int main()
{
    int n, m;
    string s, t;
    cin >> n >> m >> s >> t;

    vector<vector<vector<int> > > dp(3, vector<vector<int> >(n+1, vector<int>(m+1, INF)));
    vector<vector<vector<tuple<int, int, int> > > > prev(3, vector<vector<tuple<int, int, int> > >(n+1, vector<tuple<int, int, int> >(m+1, make_tuple(-1, -1, -1))));
    dp[0][0][0] = 0;
    for(int j=0; j<=n; ++j){
        for(int k=0; k<=m; ++k){
            for(int i=0; i<3; ++i){
                if(dp[i][j][k] == INF)
                    continue;

                if(j < n && k < m){
                    int cost = dp[i][j][k];
                    if(s[j] != t[k])
                        cost += 5;
                    if(cost < dp[0][j+1][k+1]){
                        dp[0][j+1][k+1] = cost;
                        prev[0][j+1][k+1] = make_tuple(i, j, k);
                    }
                }

                if(j < n){
                    int cost = dp[i][j][k];
                    if(i == 1)
                        cost += 2;
                    else
                        cost += 9;
                    if(cost < dp[1][j+1][k]){
                        dp[1][j+1][k] = cost;
                        prev[1][j+1][k] = make_tuple(i, j, k);
                    }
                }

                if(k < m){
                    int cost = dp[i][j][k];
                    if(i == 2)
                        cost += 2;
                    else
                        cost += 9;
                    if(cost < dp[2][j][k+1]){
                        dp[2][j][k+1] = cost;
                        prev[2][j][k+1] = make_tuple(i, j, k);
                    }
                }
            }
        }
    }

    int i = 0;
    int j = n;
    int k = m;
    for(int l=0; l<3; ++l){
        if(dp[l][j][k] < dp[i][j][k])
            i = l;
    }
    cout << dp[i][j][k] << endl;

    string s2;
    string t2;
    while(!(j == 0 && k == 0)){
        if(i == 0){
            s2 += s[j-1];
            t2 += t[k-1];
        }
        else if(i == 1){
            s2 += s[j-1];
            t2 += '-';
        }
        else{
            s2 += '-';
            t2 += t[k-1];
        }

        tie(i, j, k) = prev[i][j][k];
    }
    reverse(s2.begin(), s2.end());
    reverse(t2.begin(), t2.end());
    cout << s2 << endl << t2 << endl;

    return 0;
}
0