fork download
  1. uses math;
  2. const maxn = 2000;
  3. var a,b:ansistring;
  4. k,i,j:longint;
  5. f: array[0..maxn,0..maxn] of longint;
  6. begin
  7. readln(a);
  8. readln(b);
  9. while a[length(a)]=' ' do delete(a,length(a),1);
  10. while b[length(b)]=' ' do delete(b,length(b),1);
  11. readln(k);
  12. for i:=0 to length(a) do
  13. f[i,0]:=i*k;
  14. for i:=1 to length(b) do
  15. f[0,i]:=i*k;
  16. for i:=1 to length(a) do
  17. begin
  18. for j:=1 to length(b) do
  19. begin
  20. f[i,j]:=min(min(f[i-1,j]+k,f[i,j-1]+k),f[i-1,j-1]+abs(ord(a[i])-ord(b[j])));
  21. end;
  22. end;
  23. writeln(f[length(a),length(b)]);
  24. end.
Success #stdin #stdout 0s 16088KB
stdin
cmc 
snmn 
2
stdout
10