結果
問題 | No.225 文字列変更(medium) |
ユーザー |
![]() |
提出日時 | 2015-08-16 18:42:45 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 33 ms / 5,000 ms |
コード長 | 2,054 bytes |
コンパイル時間 | 1,062 ms |
コンパイル使用メモリ | 92,072 KB |
実行使用メモリ | 7,168 KB |
最終ジャッジ日時 | 2024-12-24 10:45:44 |
合計ジャッジ時間 | 2,239 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#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;}