题目连接:http://acm.pku.edu.cn/JudgeOnline/problem?id=3356
1,这个题目背景的提法是两个字符串的编辑距离opt[i][j]表示x串长度为i的前缀和y串长度为j的前缀的最小编辑距离
2,最优子结构是:opt[i][j] = min{opt[i-1][j-1] + diff(x[i],y[j]),opt[i-1][j] + 1,opt[i][j-1]+1}
3,边界条件是:opt[0][i] = i,opt[i][0] = i;
#include <iostream>
using namespace std;
#define MAX 1001
int i,j,len1,len2,opt[MAX][MAX];
char str1[MAX],str2[MAX];
int main()
{
freopen("in.txt","r",stdin);
str1[0] = str2[0] = 1;
while(cin >> len1 >> str1+1 >> len2 >> str2+1)
{
for(i = 0;i <= len1;++i)
opt[i][0] = i;
for(j = 0;j <= len2;++j)
opt[0][j] = j;
for(i = 1;i <= len1;++i)
{
for(j = 1;j <= len2;++j)
{
opt[i][j] = opt[i-1][j-1] + int(!(str1[i] == str2[j]));
if(opt[i][j] > opt[i-1][j] + 1)
opt[i][j] = opt[i-1][j] + 1;
if(opt[i][j] > opt[i][j-1] + 1)
opt[i][j] = opt[i][j-1] + 1;
}
}
cout << opt[len1][len2] << endl;
}
return 0;
}