Advertisement
lodha1503

Untitled

Oct 4th, 2023
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     void reverseStr(string& str)
  4.     {
  5.         int len = str.length();
  6.         int n = len-1;
  7.         int i = 0;
  8.         while(i<=n)
  9.         {
  10.            
  11.             swap(str[i],str[n]);
  12.             n = n-1;
  13.             i = i+1;
  14.         }
  15.    
  16.     }
  17.  
  18.     string solve(string &str1, string &str2,int n,int m,int ind1,int ind2,string &temp,string &ans,int &min_len,vector<vector<pair<int,string>>> &dp)
  19.     {
  20.         if(ind1<0 && ind2<0)
  21.         {
  22.             if(temp.size()<=min_len)
  23.             {
  24.                 ans=temp;
  25.                 min_len=temp.size();
  26.             }
  27.             return ans;
  28.         }
  29.  
  30.         if(ind1<0)
  31.         {
  32.             for(int k=ind2;k>=0;k--)
  33.                 temp+=str2[k];
  34.             if(temp.size()<=min_len)
  35.             {
  36.                 ans=temp;
  37.                 min_len=temp.size();
  38.             }
  39.             return ans;
  40.         }
  41.  
  42.         if(ind2<0)
  43.         {
  44.             for(int k=ind1;k>=0;k--)
  45.                 temp+=str1[k];
  46.             if(temp.size()<=min_len)
  47.             {
  48.                 ans=temp;
  49.                 min_len=temp.size();
  50.             }
  51.             return ans;
  52.         }
  53.  
  54.        
  55.  
  56.         if(dp[ind1][ind2].first!=-1)
  57.             return dp[ind1][ind2].second;
  58.  
  59.  
  60.         if(str1[ind1] == str2[ind2])
  61.         {
  62.             temp+=str1[ind1];
  63.             dp[ind1][ind2]={1,solve(str1,str2,n,m,ind1-1,ind2-1,temp,ans,min_len,dp)};
  64.             temp.pop_back();
  65.             return dp[ind1][ind2].second;
  66.         }
  67.  
  68.        
  69.         temp+=str1[ind1];
  70.         string ans1=solve(str1,str2,n,m,ind1-1,ind2,temp,ans,min_len,dp);
  71.         temp.pop_back();
  72.  
  73.         temp+=str2[ind2];
  74.         string ans2=solve(str1,str2,n,m,ind1,ind2-1,temp,ans,min_len,dp);
  75.         temp.pop_back();
  76.  
  77.         if(ans1.size()<=ans2.size())
  78.             dp[ind1][ind2]={1,ans1};
  79.         else
  80.             dp[ind1][ind2]={1,ans2};
  81.            
  82.         return dp[ind1][ind2].second;
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.     string shortestCommonSupersequence(string str1, string str2)
  89.     {
  90.         int n=str1.size();
  91.         int m=str2.size();
  92.         string temp="";
  93.         string ans="";
  94.         int min_len=INT_MAX;
  95.         vector<vector<pair<int,string>>> dp(n,vector<pair<int,string>>(m,{-1,""}));
  96.         solve(str1,str2,n,m,n-1,m-1,temp,ans,min_len,dp);
  97.         reverseStr(ans);
  98.         return ans;
  99.     }
  100. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement