Advertisement
Oppenheimer

longest repeating subsequence

Aug 15th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. // longest common subsequence
  2. // [1 ,2 ,3, 4]
  3. // subsequnce (order matters but maybe not continuous)
  4. // substring or subarray .. (need to be contigous)
  5. // subset same as subsequence ..
  6. int solve(string s1){
  7.     string s2 = s1;
  8.     int sa = s1.length();
  9.     int sb = sa;
  10.     vector<vector<int>> dp (sa+1 ,vector<int>(sb+1,0));
  11.     // a represents the ath element .. b represents the knapsack present
  12.    
  13.     for(int a=1;a<=sa;a++){
  14.         for(int b=1;b<=sb;b++){
  15.             if(s1[a-1] == s2[b-1] && a!=b){
  16.             dp[a][b] = 1 + dp[a-1][b-1]; //here we can choose ith element again
  17.             }else{
  18.             dp[a][b] = max(dp[a-1][b] , dp[a][b-1]); // cant choose the ath element becuase its weight is more
  19.             }
  20.         }
  21.     }
  22.    
  23.  
  24.    
  25.    
  26.     return dp[sa][sb]; // gives to length of lcs
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement