MAGCARI

LCS

Dec 28th, 2022
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. /*
  2.     Task    : LCS
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 28 December 2022 [18:43]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int dp[110][110];
  10. char a[110],b[110];
  11. int main(){
  12.     int n,m;
  13.     scanf("%d %d",&n,&m);   // length of string a and b
  14.     scanf(" %s %s",a+1,b+1);
  15.     int mx = 0;
  16.     pair<int ,int > pos;
  17.     for(int i=1;i<=n;i++){
  18.         for(int j=1;j<=m;j++){
  19.             if(a[i] == b[j])    dp[i][j] = dp[i-1][j-1] + 1;
  20.             // Subsequence
  21.             // dp[i][j] = max({dp[i][j],dp[i-1][j],dp[i][j-1]});
  22.             if(mx < dp[i][j]){
  23.                 mx = dp[i][j];
  24.                 pos = {i,j};
  25.             }
  26.         }
  27.     }
  28.     printf("%d\n",mx);
  29.  
  30.     // Print longest common substring only
  31.     // for(int i=mx-1;i>=0;i--)
  32.     //  printf("%c",a[pos.first - i]);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment