標籤:os io for ar amp ios har dp
題目:編輯距離,給你兩個串,將已知串轉化成目標串,可以增、刪、改字母,求最小操作次數。
分析:dp,編輯距離。同最大公用子序列。注意操作位置是即時變化的。(前面都已經處理好了)
f[i][j] = f[i-1][j] 這時刪掉 str1[i],位置j+1;
f[i][j] = f[i][j-1] 這時增加 str2[j],位置j;
f[i][j] = f[i-1][j-1]+k 如果str1[i] == str2[j]這時相同k=0,否則k=1,位置j。
說明:注意是str1變成str2;變化位置是這一步時所在的位置(前面的都處理過了)。
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>char str1[24],str2[24];int dp[24][24],op[24][24];void output( int i, int j ){if ( !i && !j ) return;if ( op[i][j] == 1 ) {output( i-1, j );printf("D%c%02d",str1[i-1],j+1);}else if ( op[i][j] == 2 ) {output( i, j-1 );printf("I%c%02d",str2[j-1],j);}else if ( op[i][j] == 3 ){output( i-1, j-1 );printf("C%c%02d",str2[j-1],j);}else output( i-1, j-1 );}int main(){while ( scanf("%s",str1) && str1[0] != '#' ) {scanf("%s",str2);int l1 = strlen(str1);int l2 = strlen(str2);for ( int i = 0 ; i <= l1 ; ++ i )for ( int j = 0 ; j <= l2 ; ++ j ) {dp[i][j] = 400;op[i][j] = 0;}//初始化 for ( int i = 0 ; i <= l1 ; ++ i ) {op[i][0] = 1; dp[i][0] = i;}for ( int i = 0 ; i <= l2 ; ++ i ) {op[0][i] = 2; dp[0][i] = i;}for ( int i = 1 ; i <= l1 ; ++ i )for ( int j = 1 ; j <= l2 ; ++ j ) {if ( str1[i-1] != str2[j-1] ) {op[i][j] = 3; dp[i][j] = dp[i-1][j-1]+1;}else dp[i][j] = dp[i-1][j-1];if ( dp[i-1][j]+1 < dp[i][j] ) {op[i][j] = 1; dp[i][j] = dp[i-1][j]+1;}if ( dp[i][j-1]+1 < dp[i][j] ) {op[i][j] = 2; dp[i][j] = dp[i][j-1]+1;}}output( l1, l2 );printf("E\n");}return 0;}