题目: http://acm.pku.edu.cn/JudgeOnline/problem?id=3356 #include "stdio.h" //f(i,j)表示字符串A的前i个字符到字符串B的前j个字符匹配所需的最小改变步数 int f[1002][1002]; int min(int a,int b,int c) { int temp; temp = a <= b ? a : b; return c <= temp ? c : temp; } int main() { int i,j; int len1,len2; char str1[1002],str2[1002]; while(scanf("%d%s",&len1,str1 + 1)!=EOF) { scanf("%d%s",&len2,str2 + 1); //注意初始化 for(i=0;i<=len1;i++) f[i][0]=i; for(j=0;j<=len2;j++) f[0][j]=j; for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { //不改变...改变A...改变B if(str1[i]==str2[j]) f[i][j]=min(f[i-1][j-1],f[i-1][j]+1,f[i][j-1]+1); else f[i][j]=min(f[i-1][j-1]+1,f[i-1][j]+1,f[i][j-1]+1); } printf("%d/n",f[len1][len2]); } return 0; }