Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include<bits/stdc++.h>
- const int mod=1e9+7;
- using namespace std;
- // } Driver Code Ends
- // function to find longest common subsequence
- class Solution
- {
- public:
- //Function to find the length of longest common subsequence in two strings.
- int lcs(int n, int m, string s1, string s2)
- {
- // your code here
- // your code here
- vector<vector<int>> dp(n+1,vector<int> (m+1,0));
- for(int j=0;j<=m;j++)
- {
- dp[0][j]=0;
- }
- for(int j=0;j<=n;j++)
- {
- dp[j][0]=0;
- }
- int ans=0;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- if(s1[i-1]==s2[j-1])
- {
- dp[i][j] = 1 + dp[i-1][j-1];
- ans = max(ans,dp[i][j]);
- }
- else
- {
- dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
- }
- }
- }
- return ans;
- }
- };
- //{ Driver Code Starts.
- int main()
- {
- int t,n,m;
- cin>>t;
- while(t--)
- {
- cin>>n>>m; // Take size of both the strings as input
- string s1,s2;
- cin>>s1>>s2; // Take both the string as input
- Solution ob;
- cout << ob.lcs(n, m, s1, s2) << endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment