Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : LCS
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 28 December 2022 [18:43]
- */
- #include<bits/stdc++.h>
- using namespace std;
- int dp[110][110];
- char a[110],b[110];
- int main(){
- int n,m;
- scanf("%d %d",&n,&m); // length of string a and b
- scanf(" %s %s",a+1,b+1);
- int mx = 0;
- pair<int ,int > pos;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] + 1;
- // Subsequence
- // dp[i][j] = max({dp[i][j],dp[i-1][j],dp[i][j-1]});
- if(mx < dp[i][j]){
- mx = dp[i][j];
- pos = {i,j};
- }
- }
- }
- printf("%d\n",mx);
- // Print longest common substring only
- // for(int i=mx-1;i>=0;i--)
- // printf("%c",a[pos.first - i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment