题目
求两个字符串的最长公共子序列长度。
输入格式:
输入长度≤100的两个字符串。
输出格式:
输出两个字符串的最长公共子序列长度。
输入样例1:
ABCBDAB
BDCABA
输出样例1:
4
输入样例2:
ABACDEF
PGHIK
输出样例2:
0
答案
using namespace std;
int main()
{
int dp[105][105];
string s1,s2;
cin>>s1>>s2;
int len1=s1.length(),len2=s2.length();
fill(dp[0],dp[0]+105*105,0);
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[len1][len2];
}
4096





