链接:https://ac.nowcoder.com/acm/contest/301/E
来源:牛客网
题目描述
小乐乐有字符串str1,str2。
小乐乐想要给他们找朋友。
小乐乐想知道在这两个字符串中最多能匹配出多长的相同子串(可非连续)。
输入描述:
第一行输入字符串str1; 第二行输入字符串str2; 数据保证字符串长度小于1000,且非空,字符串仅由小写字母组成。
输出描述:
输出最长相同子串的长度。
示例1
输入
复制
asd ad
输出
复制
2
最长公共子序列;
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <cmath>
#define ll long long
#define lowbit(x) (x&(-x))
#define pi acos(-1.0)
#define N 1001
using namespace std;
int dp[N][N];
int main() {
string a,b;
cin>>a>>b;
int lena=a.length();
int lenb=b.length();
memset(dp,0,sizeof(dp));
for(int i=1; i<=lena; i++) {
for(int j=1; j<=lenb; j++) {
if(a[i-1]==b[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[lena][lenb]<<endl;
return 0;
}