[text] 1111111

Viewer

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int lcs_dp(string strings1, string strings2) {
  8.     int m1 = strings1.length(), n1 = strings2.length();
  9.     int dynamicp[m1+1][n1+1];
  10.     for(int i=0;i<=m1;i++){
  11.         for(int j=0;j<=n1;j++){
  12.             if(i==0 || j==0){
  13.                 dynamicp[i][j]=0;
  14.             }
  15.             else if(strings1[i-1]==strings2[j-1]){
  16.                 dynamicp[i][j]=dynamicp[i-1][j-1]+1;
  17.             }
  18.             else{
  19.                 dynamicp[i][j]=max(dynamicp[i-1][j],dynamicp[i][j-1]);
  20.             }
  21.         }
  22.     }
  23.     return dynamicp[m1][n1];
  24. }
  25.  
  26. int main() {
  27.     string strings1;
  28.     string strings2;
  29.     cin>>strings1;
  30.     cin>>strings2;
  31.     int common_lcs = lcs_dp(strings1, strings2);
  32.     cout << common_lcs << endl;
  33.  
  34.     return 0;
  35. }

Editor

You can edit this paste and save as new:


File Description
  • 1111111
  • Paste Code
  • 24 Mar-2023
  • 846 Bytes
You can Share it: