Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // longest common subsequence
- // [1 ,2 ,3, 4]
- // subsequnce (order matters but maybe not continuous)
- // substring or subarray .. (need to be contigous)
- // subset same as subsequence ..
- int solve(string s1 , string s2){
- int sa = s1.length();
- int sb = s2.length();
- vector<vector<int>> dp (sa+1 ,vector<int>(sb+1,0));
- // a represents the ath element .. b represents the knapsack present
- for(int a=1;a<=sa;a++){
- for(int b=1;b<=sb;b++){
- if(s1[a-1] == s2[b-1]){
- dp[a][b] = 1 + dp[a-1][b-1]; //here we can choose ith element again
- }else{
- dp[a][b] = max(dp[a-1][b] , dp[a][b-1]); // cant choose the ath element becuase its weight is more
- }
- }
- }
- int lcs = dp[sa][sb];
- int insertions = sb -lcs;
- int deletions = sa-lcs;
- return insertions + deletions;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement