結果

問題 No.225 文字列変更(medium)
ユーザー IL_mstaIL_msta
提出日時 2015-08-16 18:42:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 2,054 bytes
コンパイル時間 1,100 ms
コンパイル使用メモリ 93,504 KB
実行使用メモリ 7,220 KB
最終ジャッジ日時 2023-08-25 23:04:45
合計ジャッジ時間 2,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
4,768 KB
testcase_01 AC 25 ms
7,220 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 34 ms
6,892 KB
testcase_13 AC 36 ms
7,120 KB
testcase_14 AC 36 ms
7,132 KB
testcase_15 AC 33 ms
6,932 KB
testcase_16 AC 34 ms
7,128 KB
testcase_17 AC 33 ms
6,948 KB
testcase_18 AC 33 ms
6,864 KB
testcase_19 AC 34 ms
7,020 KB
testcase_20 AC 33 ms
7,012 KB
testcase_21 AC 34 ms
7,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
const int LenMax = 1001;
int dp[LenMax][LenMax];
int LCS(string A,string B,string* ans=NULL){
	int aLen = A.size();
	int bLen = B.size();
	for(int i = 0; i < aLen; ++i){
		dp[i][0] = 0;
	}
	for(int j = 0; j < bLen; ++j){
		dp[0][j] = 0;
	}
	for(int i = 1; i <= aLen; ++i){
		for(int j = 1; j <= bLen; ++j){
			if( A[i-1] == B[j-1] ){
				dp[i][j] = dp[i-1][j-1] + 1;
			}else{
				dp[i][j] = max( dp[i][j-1],dp[i-1][j-1] );
			}
		}
	}
	/////
	if(ans != NULL){
		string LcsStr = "";
		int a,b;
		a = aLen;
		b = bLen;
		while( a > 0 && b > 0){
			if( A[a-1] == B[b-1] ){
				LcsStr = A[a-1] + LcsStr;
				--a;--b;
			}else{
				if( dp[a][b-1] > dp[a-1][b] ){
					--b;
				}else{
					--a;
				}
			}
		}
		*ans = LcsStr;
	}
	/////
	return dp[aLen][bLen];
}

int levenshtein_distance(string A,string B){
	int aLen,bLen,X;
	aLen = A.size();
	bLen = B.size();
	for(int i=0; i<=aLen; ++i){
		dp[i][0] = i;
	}
	for(int j=0; j<=bLen; ++j){
		dp[0][j] = j;
	}
	for(int i=1;i<=aLen;++i){
		for(int j=1;j<=bLen;++j){
			X = A[i-1]==B[j-1] ? 0 : 1 ;
			vector<int> vec(3);
			vec[0] = dp[i-1][j]+1;
			vec[1] = dp[i][j-1]+1;
			vec[2] = dp[i-1][j-1]+X;
			sort(vec.begin(),vec.end());
			dp[i][j] = vec[0];
		}
	}
	return dp[aLen][bLen];
}

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	int n,m;
	string S,T;
	cin>>n>>m>>S>>T;
	//int Len;
	//string LcsStr="";
	//Len = LCS(S,T,&LcsStr);
	
	int dis;
	dis = levenshtein_distance(S,T);
	
	P(dis);
	return 0;
}
0