結果

問題 No.225 文字列変更(medium)
ユーザー codershifthcodershifth
提出日時 2016-01-28 09:58:59
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,530 bytes
コンパイル時間 1,295 ms
コンパイル使用メモリ 165,148 KB
実行使用メモリ 6,892 KB
最終ジャッジ日時 2023-10-21 16:29:43
合計ジャッジ時間 2,506 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,780 KB
testcase_01 AC 5 ms
5,836 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 9 ms
6,628 KB
testcase_13 WA -
testcase_14 AC 8 ms
6,892 KB
testcase_15 AC 12 ms
6,628 KB
testcase_16 AC 8 ms
6,892 KB
testcase_17 AC 12 ms
6,628 KB
testcase_18 AC 10 ms
6,628 KB
testcase_19 AC 8 ms
6,628 KB
testcase_20 WA -
testcase_21 AC 9 ms
6,628 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));
            if ( S[0] == T[0] )
                dp[0][0] = 0;
            else
                dp[0][0] = 1;

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

                if (i+1 < n && j+1 < m)
                {
                    if (S[i+1] == T[j+1])
                        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
                    else
                        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + 1); // 置換が必要
                }
            }
            cout<<dp[n-1][m-1]<<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