結果

問題 No.225 文字列変更(medium)
ユーザー codershifthcodershifth
提出日時 2016-03-13 14:33:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,489 bytes
コンパイル時間 1,320 ms
コンパイル使用メモリ 149,344 KB
実行使用メモリ 6,776 KB
最終ジャッジ日時 2023-08-25 23:06:44
合計ジャッジ時間 2,018 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,660 KB
testcase_01 AC 5 ms
5,624 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 7 ms
6,452 KB
testcase_13 AC 7 ms
6,776 KB
testcase_14 AC 7 ms
6,772 KB
testcase_15 AC 6 ms
6,480 KB
testcase_16 AC 6 ms
6,776 KB
testcase_17 AC 5 ms
6,652 KB
testcase_18 AC 6 ms
6,424 KB
testcase_19 AC 6 ms
6,464 KB
testcase_20 AC 6 ms
6,432 KB
testcase_21 AC 6 ms
6,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class ChangeStringMedium
{
public:
    void solve(void)
    {
            int n,m;
            cin>>n>>m;
            string S,T;
            cin>>S>>T;

            const int inf = (1<<30);

            // ルーベンシュタインの編集距離
            // dp[i][j] := S[0...i] から T[0...j] へ変更するときの最小操作回数
            vector<vector<int>> dp(n+1,vector<int>(m+1,inf));

            // 空文字からの距離は挿入分の長さ
            REP(i,n+1)
                dp[i][0] = i;
            REP(j,m+1)
                dp[0][j] = j;

            // O(n*m)
            FOR(i,1,n+1)
            FOR(j,1,m+1)
            {
                dp[i][j] = min(dp[i][j], dp[i-1][j]+1); // 削除が必要
                dp[i][j] = min(dp[i][j], dp[i][j-1]+1); // 挿入が必要

                if (S[i-1] == T[j-1])
                    dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
                else
                    dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 1); // 置換が必要
            }
            cout<<dp[n][m]<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new ChangeStringMedium();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0