Advertisement
XuanHong

LTTS - tìm xâu con chung dài nhất

Jan 26th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. // LTTS - tìm xâu con chung dài nhất
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. string x;
  9. string y;
  10.  
  11. int a[100][100] = {0};
  12.  
  13. int main()
  14. {  
  15.     cout<<"Nhap chuoi 1:\n";
  16.     cin>>x;
  17.     cout<<"Nhap chuoi 2:\n";
  18.     cin>>y;
  19.  
  20.     int max=0; 
  21.  
  22.     for(int i=1; i<x.size()+1; i++)
  23.     {
  24.         for(int j=1; j<y.size()+1; j++)
  25.         {
  26.             if(x[i-1]==y[j-1])
  27.             {
  28.                 a[i][j]=a[i-1][j-1]+1;
  29.                 if(max<a[i][j])
  30.                 {
  31.                     max = a[i][j];
  32.                 }
  33.             }
  34.             else
  35.                 a[i][j]=0;
  36.         }
  37.     }
  38.    
  39.     cout<<max<<endl;
  40.        
  41.     for(int i=1; i<x.size()+1; i++)
  42.     {
  43.         for(int j=1; j<y.size()+1; j++)
  44.         {
  45.             if(a[i][j]==max)
  46.             {
  47.                 for(int k=i-max; k<i; k++)
  48.                 {
  49.                     cout<<x[k];
  50.                 }
  51.                 cout<<endl;
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement