题目
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.
Input
Input 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.
Output
For 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 Input
abcac
bcd
aaa
aabaaaa
Sample 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
因为要输出路径,所以不能用滚动数组,要用二维数组
先把最短编辑距离求出来,再根据替换、修改、删除或者不变的性质 回推 注意数组越界问题
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int f[100][100]; //f[i][j]表示从a[1...i]到b[1...j]的编辑距离
/*
1.若a[i] = b[j],则f[i][j] = f[i-1][j-1]
2.若a[i] ≠ b[j],考虑修改,插入和删除的编辑距离的最小值
① 修改操作:把a[i]改为b[j]
f[i][j] = f[i-1][j-1] + 1
② 插入操作:在a[i]后插入b[i]
f[i][j] = f[i][j-1] + 1
③ 删除操作:删除a[i]
f[i][j] = f[i-1][j] + 1
*/
string a,b;
int n,m,cnt;
void ptf() //倒推回去
{
int i = a.length();
int j = b.length();
cnt = 0;
while(i > 0 || j > 0)
{
if(i > 0 && j == 0)
{
printf("%d Delete %d\n",++cnt,i); // 避免数组越界
i--;
continue;
}
else if(i == 0 && j > 0)
{
printf("%d Insert %d,%c\n",++cnt,i+1,b[j-1]); // 避免数组越界
j--;
continue;
}
else
{
if(f[i][j] == f[i-1][j-1] && a[i-1] == b[j-1]) // 当前位置一样,不动
{
i--,j--;
continue;
}
if(f[i][j] == f[i-1][j-1] + 1) // 替换
{
printf("%d Replace %d,%c\n",++cnt,i,b[j-1]);
i--,j--;
continue;
}
if(f[i][j] == f[i][j-1] + 1) // 插入在该位置前面
{
printf("%d Insert %d,%c\n",++cnt,i+1,b[j-1]);
j--;
continue;
}
if(f[i][j] == f[i-1][j] + 1) // 删除
{
printf("%d Delete %d\n",++cnt,i);
i--;
continue;
}
}
}
}
int main()
{
while(cin >> a >> b)
{
m = a.length(); //当前字符串
n = b.length(); //目标字符串
for(int i = 1; i <= m; i++) f[i][0] = i;
for(int j = 1; j <= n; j++) f[0][j] = j;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[i-1] == b[j-1])
{
f[i][j] = f[i-1][j-1];
}
else
{
f[i][j] = min(f[i-1][j-1],min(f[i][j-1],f[i-1][j])) + 1;
}
}
}
cout << f[m][n] << endl;
ptf();
memset(f,0,sizeof(f));
}
return 0;
}