Advertisement
rotti321

Levenstein [siruri2]

Mar 12th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. # include <fstream>
  2. # include <cstring>
  3. # include <algorithm>
  4. # define NR 505
  5. using namespace std;
  6. ifstream f("siruri2.in");
  7. ofstream g("siruri2.out");
  8. int i,j,n,m;
  9. int a[NR][NR];
  10. char s1[NR], s2[NR];
  11. int minim (int a, int b, int c)
  12. {
  13.     return min(a, min(b, c));
  14. }
  15. int main ()
  16. {
  17.     f.getline (s1+1, NR); n=strlen(s1+1);
  18.     f.getline (s2+1, NR); m=strlen(s2+1);
  19.  
  20.     ///init
  21.     for (i=1; i<=n; ++i)
  22.     {
  23.         a[i][0]=i;
  24.         if ('A'<=s1[i] && s1[i]<='Z')
  25.         {
  26.             s1[i]=s1[i]-'A'+'a';
  27.         }
  28.     }
  29.     for (i=1; i<=m; ++i)
  30.     {
  31.         a[0][i]=i;
  32.         if ('A'<=s2[i] && s2[i]<='Z') s2[i]=s2[i]-'A'+'a';
  33.     }
  34.     for (j=1; j<=m; ++j)
  35.     {
  36.         for (i=1; i<=n; ++i)
  37.         {
  38.             if (s1[i]==s2[j]) a[i][j]=a[i-1][j-1];
  39.             else {
  40.                     ///adaugam un element
  41.                     a[i][j]=minim (a[i-1][j]+1, a[i][j-1]+1, a[i-1][j-1]+1);
  42.                  }
  43.         }
  44.     }
  45.     g<<a[n][m];
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement