結果

問題 No.225 文字列変更(medium)
ユーザー vain0
提出日時 2015-06-12 23:41:13
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 1,515 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 80,028 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-12-24 10:11:38
合計ジャッジ時間 1,906 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <functional>
#include <set>
#include <ctime>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#ifndef ONLINE_JUDGE //POJ
# include <complex>
# include <random>
# include <array>
# define mkt make_tuple
# define empb emplace_back
#endif
#ifdef _LOCAL
# include "for_local.h"
#endif
using namespace std;
typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull;
#define repi(_I, _B, _E) for(int _I = (_B); (_I) < (_E); ++ (_I))
#define rep(_I, _N) for(int _I = 0; (_I) < (_N); ++ (_I))
#define mkp make_pair
#define all(_X) (_X).begin(), (_X).end()
inline int scani() { int n; scanf("%d", &n); return n; }

int const INF = 1<<29;
int n,m;
string s, t;
int const N = 1000 + 1;

#if 1
int memo[N][N][2];
int dfs(int is, int it, int k) {
	if ( is == n || it == m ) return (n - is) + (m - it);

	auto& x = memo[is][it][k];
	if ( x != INF + 1 ) { return x; }

	if ( s[is] == t[it] ) {
		return x = dfs(is + 1, it + 1, k);
	} else {
		return x =
			min(dfs(is + 1, it + 1, k) + 1,
			min(dfs(is + 1, it, 1 - k) + 1,
				dfs(is, it + 1, 1 - k) + 1));
	}
}
int calc() {
	rep(i, N) rep(j, N) rep(k, 2) memo[i][j][k] = INF + 1;
	return dfs(0, 0, (m - n) & 1);
}
#endif

signed main() {
	cin >> n >> m >> s >> t;
	if ( n > m ) { swap(n, m); swap(s, t); }
	s.append("*"); t.append("*");

	int const c = calc();
	cout << c << endl;
	return 0;
}
0