Combine String(dp LCS变形)

20 篇文章 0 订阅
8 篇文章 0 订阅

Problem Description
Given three strings , and , your mission is to check whether is the combine string of and .
A string is said to be the combine string of and if and only if can be broken into two subsequences, when you read them as a string, one equals to , and the other equals to .
For example, “adebcf”’ is a combine string of “abc’” and “def”.

Input
Input file contains several test cases (no more than 20). Process to the end of file.
Each test case contains three strings , and (the length of each string is between 1 and 2000).

Output
For each test case, print ”Yes“, if is a combine string of and , otherwise print “No”.

*Sample Input
abc
def
adebcf
abc
def
abecdf

Sample Output
Yes
No
题意: 多组输入,输入三个字符串,判断字符串c能否拆分成字符串a和b,可以输出Yes,否则输出No。
思路: 因为是多组输入且字符串长度没有给定,直接暴力的话一般会超时.
结合LCS(最长公共子序列)的知识,我们可以使用dp来做这道题;

用数组dp[i][j]?=1来表示字符串a的第i位、字符串b的第j位与字符串c的[i+j]是否匹配;(前提是dp[i-1][j]或者dp[i][j-1]是否为1,也就是它的前一位是否有a或b去匹配)

最终我们只需要检查下dp[a.size][b.size]是否为1.

注意字符数组的大小,或者直接用string

写法一(LCS模板):
**LCS算法:**https://blog.csdn.net/lz161530245/article/details/76943991

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
int dp[4010][4010];
char a[4010],b[4010],c[4010];
int main()
{
	int l1,l2,l3,i,j;
    while(~scanf("%s",a))
    {
        memset(dp,0,sizeof(dp));
        scanf("%s",b);
        scanf("%s",c);
        l1=strlen(a);
        l2=strlen(b);
        l3=strlen(c);
        if(l1+l2!=l3)
        {
        	printf("No\n");
        	continue;
        }
        dp[0][0]=1;
        for(i=1;i<=l1;i++)
		{
			if(a[i-1]==c[i-=1]&&dp[0][i-1]==1)
				dp[0][i]=1;
			else
				break;
		}
		
		for(i=1;i<=l2;i++){
			if(b[i-1]==c[i-1]&&dp[i-1][0]==1)
				dp[i-1][0]=1;
			else
				break;
		}
		
		for(i=1;i<=l2;i++)
			for(j==1;j<=l1;j++)
			{
				if((dp[i-1][j]==1&&a[j-1]==c[i+j-1])||(dp[i][j-1]==1&&b[i-1]==c[i+j-1]))
					dp[i][j]=1;
				else
					break;
			}
        if(dp[l1][l2]!=0)
           printf("Yes\n");
        else
           printf("No\n");
    }
    return 0;
}

写法二(简写):

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
int dp[4010][4010];
char a[4010],b[4010],c[4010];
int main()
{
	int l1,l2,l3,i,j;
    while(~scanf("%s",a+1))
    {
        memset(dp,0,sizeof(dp));
        scanf("%s",b+1);
        scanf("%s",c+1);
        l1=strlen(a+1);
        l2=strlen(b+1);
        l3=strlen(c+1);
        if(l1+l2!=l3)
        {
        	printf("No\n");
        	continue;
        }
        dp[0][0]=1;
        for(i=0;i<=l1;i++)
           for(j=0;j<=l2;j++)
           {
           	    if((i>0&&dp[i-1][j]&&a[i]==c[i+j])||(j>0&&dp[i][j-1]&&b[j]==c[i+j]))
           	       dp[i][j]=1;
           }
        if(dp[l1][l2]!=0)
           printf("Yes\n");
        else
           printf("No\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值