fork(1) download
  1. #include<iostream>
  2. #include<string>
  3. #include<stdio.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8. int t;unsigned int m,n;
  9. int **edit=new int*[2001]; bool flag;
  10. for(unsigned int k=0;k<2001;k++){edit[k]=new int[2001];}
  11.  
  12. string::iterator i,j;
  13. scanf("%d",&t);getchar();
  14. while(t--)//test cases
  15. {
  16. string *a=new string;
  17. string *b=new string;
  18. getline(cin,*a);m=(*a).length()+1;//cout<<m;
  19. getline(cin,*b);n=(*b).length()+1;//cout<<n;
  20.  
  21. for(unsigned int x=0;x<m;x++)
  22. {
  23. for(unsigned int y=0;y<n;y++)
  24. {
  25. if(x==0&&y==0){edit[x][y]=0;}
  26. else if(x==0){edit[x][y]=edit[x][y-1]+1;}//insert operation
  27. else if(y==0){edit[x][y]=edit[x-1][y]+1;}//delete operation
  28. else
  29. { i=(*a).begin()+x-1;j=(*b).begin()+y-1;
  30. if(*i==*j){ flag=0;}
  31. else {flag=1;}
  32. edit[x][y]=min(min(edit[x-1][y-1]+flag,edit[x-1][y]+1),edit[x][y-1]+1);
  33.  
  34. }
  35. }
  36. }
  37.  
  38. printf("%d\n",edit[m-1][n-1]);
  39. delete a;
  40. delete b;
  41. }
  42. return 0;
  43. }
  44.  
  45.  
Success #stdin #stdout 0s 19040KB
stdin
4
FOOD
MONEY
PLASMA
ALTRUISM
SPARTAN
PART
ANKUR
PRANKUR
stdout
4
6
3
2