求两个字符串的最长公共子序列长度。
输入格式:
输入长度≤100的两个字符串。
输出格式:
输出两个字符串的最长公共子序列长度。
输入样例1:
ABCBDAB
BDCABA
输出样例1:
4
输入样例2:
ABACDEF
PGHIK
输出样例2:
0
代码
#include <iostream>
#include <string>
using namespace std;
int a[100][100], length1, length2;
string s1, s2;
void DP()
{
for (int i = 1; i <= length1; i++)
{
for (int j = 1; j <= length2; j++)
{
if (s1[i - 1] == s2[j - 1])
a[i][j] = a[i - 1][j - 1] + 1;
else
a[i][j] = max(a[i][j - 1], a[i - 1][j]);
}
}
}
int main()
{
getline(cin, s1);
getline(cin, s2);
length1 = s1.length();
length2 = s2.length();
DP();
cout << a[length1][length2];
return 0;
}