typedef long long ll; #include<iostream> #include<vector> using namespace std; const int…
- User
typedef long long ll;
#include #include
using namespace std;
const int MOD=1e9+7;
int main() { string str1,str2; cin>>str1>>str2; int m=str1.size(),n=str2.size(); vector<vector> dp(m+1,vector(n+1,0));
for(ll i=1;i<=m;i++){ for(ll j=1;j<=n;j++){ if(str1[i-1]==str2[j-1]){ dp[i][j]=dp[i-1][j-1]+1; } else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } string ans=""; int idx1=m,idx2=n; while(idx1>0 && idx2>0){ if(str1[idx1]==str2[idx2]){ ans+=str1[idx1]; idx1--; idx2--; } else if(dp[idx1-1][idx2]>dp[idx1][idx2-1]){ idx1--; } else{ idx2--; } } reverse(ans.begin(),ans.end()); cout<<ans<<endl; return 0;}
is this correct for LCS ?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.