UVA 10405 - Longest Common Subsequence




#include <bits/stdc++.h>
using namespace std;
int t[1005][1005];
int main()
{
   string a,b;
   char str1[1005],str2[1005];
   int l1,l2,i,j;
   while(gets(str1))
   {
    gets(str2);
    l1=strlen(str1);
    l2=strlen(str2);
    for(i=0;i<=l1;i++)
    {
        t[i][0]=0;
    }
    for(j=0;j<=l2;j++)
    {
        t[0][j]=0;
    }
    for(i=1;i<=l1;i++)
    {
        for(j=1;j<=l2;j++)
        {
            if(str1[i-1]==str2[j-1])
            {
                t[i][j]=t[i-1][j-1]+1;
            }
            else if(str1[i-1]!=str2[j-1])
            {
                t[i][j]=max(t[i-1][j],t[i][j-1]);
            }
        }
    }
    cout<<t[l1][l2]<<endl;
    }
    return 0;


}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment