HDU 1516 String Distance and Transform Process
String Distance is a non-negative integer that measures the distance between two strings. Here we give the definition. A transform list is a list of string, where each string, except for the last one, can be changed to the string followed by adding a character, deleting a character or replacing a character. The length of a transform list is the count of strings minus 1 (that is the count of operations to transform these two strings). The distance between two strings is the length of a transform list from one string to the other with the minimal length. You are to write a program to calculate the distance between two strings and give the corresponding transform list.InputInput consists a sequence of string pairs, each string pair consists two lines, each string occupies one line. The length of each string will be no more than 80.
OutputFor each string pair, you should give an integer to indicate the distance between them at the first line, and give a sequence of command to transform string 1 to string 2. Each command is a line lead by command count, then the command. A command must be
Insert pos,value
Delete pos
Replace pos,value
where pos is the position of the string and pos should be between 1 and the current length of the string (in Insert command, pos can be 1 greater than the length), and value is a character. Actually many command lists can satisfy the request, but only one of them is required.
Sample Inputabcac bcd aaa aabaaaaSample Output
3 1 Delete 1 2 Replace 3,d 3 Delete 4 4 1 Insert 1,a 2 Insert 2,a 3 Insert 3,b 4 Insert 7,a
dp求出字符串的编辑距离,dp[m][n]指字符串的前m位变为字符串b的前n位
初始化dp数组只需要dp[0][i]=i,dp[i][0]=i
dp[m][n]和的dp[m-1][n],dp[m][n-1],dp[m-1][n-1]相关,分别代表删,增,改,注意如果改的两个字符一样则不需要加一,否则取最小值加一即可
然后用反向的推导,输出编辑的路径
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<cstdlib> #include<cmath> #include<queue> using namespace std; typedef long long ll; #define Inf 99999999 #define Maxn 85 int main() { char a[Maxn], b[Maxn]; int dp[Maxn][Maxn] = { 0 }; while (~scanf("%s%s", a, b)) { int m = strlen(a); int n = strlen(b); for (int i = 0; i <= m; i++) { dp[i][0] = i; } for (int i = 0; i <= n; i++) { dp[0][i] = i; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1; dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (a[i - 1] != b[j - 1])); } } cout << dp[m][n] << endl; int x = 1; int t = 0; while (m >= 1 || n >= 1) { if (a[m - 1] != b[n - 1]) { t = 1; } else { t = 0; } if (dp[m][n] == (dp[m - 1][n - 1] + t) && m >= 1 && n >= 1) { if(t) cout << x++ << " Replace " << m << "," << b[n - 1] << endl; m--; n--; } else if (dp[m][n] == (dp[m][n - 1] + 1) && n >= 1) { cout << x++ << " Insert " << m + 1 << "," << b[n - 1] << endl; n--; } else if (dp[m][n] == (dp[m - 1][n] + 1 )&& m >= 1) { cout << x++ << " Delete " << m << endl; m--; } } } return 0; }

浙公网安备 33010602011771号