結果

問題 No.225 文字列変更(medium)
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-09-28 14:56:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,353 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 160,664 KB
実行使用メモリ 7,252 KB
最終ジャッジ日時 2024-04-10 06:09:26
合計ジャッジ時間 2,344 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 6 ms
7,252 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 6 ms
7,040 KB
testcase_13 AC 7 ms
7,144 KB
testcase_14 AC 7 ms
7,040 KB
testcase_15 AC 7 ms
7,012 KB
testcase_16 AC 7 ms
7,124 KB
testcase_17 AC 6 ms
6,940 KB
testcase_18 AC 7 ms
6,940 KB
testcase_19 AC 7 ms
7,220 KB
testcase_20 AC 6 ms
6,988 KB
testcase_21 AC 7 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < int(n); i++)
#define FOR(i,n,m) for(int i = int(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

int LP[1005][1005] = {};

int main() {
    int n, m; cin >> n >> m;
    int j, k;
    string x, y;
    cin >> x >> y;
    for (j = 1; j <= x.size(); j++) LP[j][0] = j;
    for (k = 1; k <= y.size(); k++) LP[0][k] = k;

    //aをbに近づけたい!
    for (j = 1; j <= x.size(); j++) {
        for (k = 1; k <= y.size(); k++) {
            //a[j]を削除するか、a[j+1]にb[k]と同じ文字を挿入するか
            //上記2つの行為の回数で最小な方を採用
            int m = min(LP[j - 1][k] + 1, LP[j][k - 1] + 1);
            if (x[j - 1] == y[k - 1]) {
                //最後の文字が同じだから最後の文字がなくても編集距離は同じ
                m = min(m, LP[j - 1][k - 1]);
                LP[j][k] = m;
            }
            else {
                //最後の文字を置換する
                m = min(m, LP[j - 1][k - 1] + 1);
                LP[j][k] = m;
            }
        }
    }
    cout << LP[x.size()][y.size()] << endl;
    return 0;
}
0