比赛 20090916练习赛 评测结果 AAAAAAAAAA
题目名称 字符串的距离 最终得分 100
用户昵称 raywzy 运行时间 0.193 s
代码语言 C++ 内存使用 18.55 MiB
提交时间 2013-11-07 20:05:15
显示代码纯文本
  1. #include<fstream>
  2. #include<string>
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<cstring>
  6. using namespace std;
  7. ifstream fin("blast.in");
  8. ofstream fout("blast.out");
  9. int K;
  10. char a[2001],b[2001];
  11. string A,B;
  12. int f[2001][2001];
  13. int min(int a,int b,int c)
  14. {
  15. int temp;
  16. if(a>b) temp=b; else temp=a;
  17. if(temp>c) return c;else return temp;
  18. }
  19. int main()
  20. {
  21. int LA,LB;
  22. fin>>A>>B>>K;
  23. LA=A.length();LB=B.length();
  24. int i,j;
  25. int temp=0;
  26. for(i=1;i<=LA;i++)
  27. {
  28. f[i][0]=i*K;
  29. a[i]=A[i-1];
  30. }
  31. for(i=1;i<=LB;i++)
  32. {
  33. f[0][i]=i*K;
  34. b[i]=B[i-1];
  35. }
  36. for(i=1;i<=LA;i++)
  37. for(j=1;j<=LB;j++)
  38. {
  39. f[i][j]=min(f[i-1][j]+K,f[i][j-1]+K,f[i-1][j-1]+abs(int(a[i])-int(b[j])));
  40. }
  41. fout<<f[LA][LB]<<endl;
  42. return 0;
  43. }