結果

問題 No.225 文字列変更(medium)
ユーザー syarosyaro
提出日時 2018-01-10 20:52:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 3,343 bytes
コンパイル時間 1,812 ms
コンパイル使用メモリ 172,980 KB
実行使用メモリ 10,740 KB
最終ジャッジ日時 2023-08-25 10:38:23
合計ジャッジ時間 3,073 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,996 KB
testcase_01 AC 7 ms
8,072 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 10 ms
9,956 KB
testcase_13 AC 10 ms
10,740 KB
testcase_14 AC 10 ms
10,432 KB
testcase_15 AC 9 ms
10,220 KB
testcase_16 AC 10 ms
10,168 KB
testcase_17 AC 9 ms
10,208 KB
testcase_18 AC 9 ms
9,924 KB
testcase_19 AC 10 ms
10,164 KB
testcase_20 AC 9 ms
9,652 KB
testcase_21 AC 10 ms
10,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/problems/610
#include <bits/stdc++.h>
using namespace std;

#define times(n, i)      uptil(0, n, i)
#define rtimes(n, i)     downto((n) - 1, 0, i)
#define upto(f, t, i)    for(decltype(t) i##0_to = (t), i = (f); i <= i##0_to; i++)
#define uptil(f, t, i)   for(decltype(t) i##0_to = (t), i = (f); i <  i##0_to; i++)
#define downto(f, t, i)  for(decltype(f) i##0_to = (t), i = (f); i >= i##0_to; i--)
#define downtil(f, t, i) for(decltype(f) i##0_to = (t), i = (f); i >  i##0_to; i--)
// types
    typedef long double LD;
    #define long long long
    #define int long
    using VI = vector<int>;
    using WI = vector<VI>;
    using PI = pair<int, int>;
    using VPI = vector<PI>;
    using WPI = vector<VPI>;

bool debug;
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 2
#define _LIBCPP_DEBUG2 2
#define ln << '\n'
#define tb << '\t'
#define sp << ' '
#define db(x) if(debug) cerr << #x << " = " << (x) << ", "
#define dbg(x) if(debug) cerr << #x << " = " << (x) ln

constexpr long INF = 1LL << 60;

void solve();

signed main(signed argc, char *argv[]) {
    #ifdef EBUG
        debug = true;
    #elif defined(ONLINE_JUDGE)
        debug = false;
    #else
        debug = argc >= 2;
    #endif
    if(!debug) {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);

    solve();

    return 0;
}

/******************************* basic library ********************************/
// IO
    template<class T>
    istream& operator>>(istream& s, vector<T>& v) {
        for(auto&& p : v) s >> p; return s;
    }
    template<class T>
    ostream& operator<<(ostream& s, vector<T> v) {
        int i = 0;
        for(const auto& p : v) s << (i++?" ":"") << p;
        return s;
    }
    template<class T>
    ostream& operator<<(ostream& s, vector<vector<T>> w) {
        for(const auto& v : w) {
            int i = 0;
            for(const auto& p : v) s << (i++?" ":"") << p;
            s ln;
        }
        return s;
    }
    template<class T, class... A>
    T RD(A... a) { T t(a...); cin >> t; return t; }
// other
#define all(x) (x).begin(), (x).end()
#define b_max(x, y) x = max(x, y)
#define b_min(x, y) x = min(x, y)
inline LD AC(LD d) { return d ? d : 0; }

/****************************** optional library ******************************/
//-mod constexpr long MOD = 1000000009; // 1000000007; // 998244353;
//-math
//-rmq
//-dfs
//-sparse_table(fn,nil=min,INF)

//+string/edit_distance
	template<class T> int edit_distance(
		vector<T> a, vector<T> b,
		int ins = 1, int sub = 1, int pass = 0
	) {
		if(ins < 0) { db(ins); dbg(ins >= 0); }
		if(sub < 0) { db(sub); dbg(sub >= 0); }
		if(pass > 0) { db(pass); dbg(sub <= 0); }
		int x = a.size(), y = b.size();
		WI dp(x + 1, VI(y + 1));
		upto(1, x, i) dp[i][0] = dp[i-1][0] + ins;
		upto(1, y, j) dp[0][j] = dp[0][j-1] + ins;
		upto(1, x, i) upto(1, y, j) {
			dp[i][j] = min({
				dp[i-1][j] + ins,
				dp[i][j-1] + ins,
				dp[i-1][j-1] + (a[i-1] == b[j-1] ? pass : sub),
			});
		}

		return dp[x][y];
	}


/************************************ main ************************************/


void solve() {
    auto N = RD<int>(), M = RD<int>();
    auto S = RD<string>(), T = RD<string>();
    vector<char> s(all(S)), t(all(T));
    cout << edit_distance(s, t) ln;
}
0