Advertisement
hugol

Untitled

Mar 27th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1.  
  2.  
  3. //* WIKIPEDIA *//
  4.  
  5. /*int main()
  6. {
  7.     string A,B;
  8.  
  9.     cin >> A;
  10.     cin >> B;
  11.  
  12.     int **C = new int*[A.length()+1];
  13.  
  14.     for (int i=0; i<A.length()+1; i++)
  15.     {
  16.         C[i] = new int[B.length()+1];
  17.         C[i][0] = 0;
  18.         C[i][1] = 0;
  19.     }
  20.  
  21.     for (int i=0; i<B.length()+1; i++)
  22.     {
  23.         C[0][i] = 0;
  24.         C[1][i] = 0;
  25.     }
  26.  
  27.     for(int i=1; i<A.length()+1; i++)
  28.     {
  29.         for(int j=1; j<B.length()+1; j++)
  30.             if (A[i-1] == B[j-1])
  31.             {
  32.                 C[i][j] = C[i-1][j-1] + 1;
  33.             }
  34.             else
  35.             {
  36.                 C[i][j] = max(C[i-1][j], C[i][j-1]);
  37.             }
  38.     }
  39.  
  40.     string R;
  41.     int x = B.length();
  42.     int y = A.length();
  43.     while(x!=0 && y!=0)
  44.     {
  45.         if (C[y][x] == C[y-1][x])
  46.         {
  47.             --y;
  48.         } else if (C[y][x] == C[y][x-1])
  49.         {
  50.             --x;
  51.         } else
  52.         {
  53.             R.push_back(A[y-1]);
  54.             --x; --y;
  55.         }
  56.     }
  57.     reverse(R.begin(), R.end());
  58.     cout << R << endl;
  59.  
  60.     /*
  61.     // wypisywanie i zwalnianie
  62.     cout << "A\\B\t";
  63.     for(int i=0; i<B.length(); i++)
  64.     {
  65.         cout << B[i] << '\t';
  66.     }
  67.     cout << "\n\n";
  68.  
  69.     for(int i=1; i<A.length()+1; i++)
  70.     {
  71.         cout << A[i-1] << '\t';
  72.         for(int j=1; j<B.length()+1; j++)
  73.         {
  74.             cout << C[i][j] << '\t';
  75.         }
  76.         delete []  C[i];
  77.         cout << endl;
  78.     }
  79.     delete [] C;
  80.    
  81.  
  82.     return 0;
  83. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement