哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)E 小乐乐匹配字符串

链接: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值