2020ICPC·小米 网络选拔赛第二场 Subsequence Pair(lcs应用)

题目链接:点击这里

题目大意:
给出两个字符串 s , t s,t s,t ,记 s s s 的子序列为 s ′ s' s t t t 的子序列为 t ′ t' t ,求 m a x ( ∣ s ′ ∣ + ∣ t ′ ∣ ) max(|s'|+|t'|) max(s+t)

题目分析:
看到答案跟二者子序列有关,就会很容易的联想到最长公共子序列,在 d p dp dp 过程中维护答案即可
维护方式:
l s = ∣ s ∣ , l t = ∣ t ∣ , d p [ i ] [ j ] 为 l c s 数 组 ls=|s|,lt=|t|,dp[i][j]为lcs数组 ls=s,lt=t,dp[i][j]lcs
s i < t j s_i < t_j si<tj 时,可以取 d p [ i − 1 ] [ j − 1 ] dp[i-1][j-1] dp[i1][j1] 作为前面相同的元素,因为 s i < t j s_i < t_j si<tj 了所以 s i s_i si t j t_j tj 后面元素的选取就变成了任意的,因为要尽可能让子序列更长所以我们就把后面的全选上,于是此时 a n s = m a x ( a n s , d p [ i − 1 ] [ j − 1 ] ∗ 2 + l s − i + 1 + l t − j + 1 ) ans = max(ans,dp[i-1][j-1]*2+ls-i+1+lt-j+1) ans=max(ans,dp[i1][j1]2+lsi+1+ltj+1)
对于任意的 s i s_i si t j t_j tj 都有 a n s = m a x ( a n s , d p [ i ] [ j ] ∗ 2 + l t − j ) ans = max(ans,dp[i][j]*2+lt-j) ans=max(ans,dp[i][j]2+ltj) ,因为空串的字典序是比任意串的字典序都小的

具体细节见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 2e3+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
char s[maxn],t[maxn];
int dp[maxn][maxn];
int main()
{
	while(~scanf("%s%s",s+1,t+1))
	{
		int ls = strlen(s+1),lt = strlen(t+1),ans = 0;
		memset(dp,0,sizeof(dp));
		for(int i = 0;i <= ls;i++)
			for(int j = 0;j <= lt;j++)
			{
				if(i && j)
				{
					dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
					if(s[i] == t[j]) dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
					else if(s[i] < t[j]) ans = max(ans,dp[i-1][j-1]*2+ls-i+1+lt-j+1);
				}
				ans = max(ans,dp[i][j]*2+lt-j);
			}
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值