#include<bits/stdc++.h>
#define ll long int
using namespace std;
ll lcs(string s1, string s2, ll n, ll m)
{
ll L[n+1][m+1];
ll i, j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i == 0 || j == 0)
L[i][j] = 0;
else if(s1[i-1] == s2[j-1])
{
L[i][j] = 1 + L[i-1][j-1];
}
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[n][m];
}
int main()
{
string s1, s2;
while(getline(cin, s1) && getline(cin, s2))
{
ll n = s1.size();
ll m = s2.size();
cout << lcs(s1, s2, n, m) << endl;
}
return 0;
}
#define ll long int
using namespace std;
ll lcs(string s1, string s2, ll n, ll m)
{
ll L[n+1][m+1];
ll i, j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i == 0 || j == 0)
L[i][j] = 0;
else if(s1[i-1] == s2[j-1])
{
L[i][j] = 1 + L[i-1][j-1];
}
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[n][m];
}
int main()
{
string s1, s2;
while(getline(cin, s1) && getline(cin, s2))
{
ll n = s1.size();
ll m = s2.size();
cout << lcs(s1, s2, n, m) << endl;
}
return 0;
}
Comments
Post a Comment