dp两个字符串比较一个字符串

Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. 
A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read them as a string, one equals to aa, and the other equals to bb. 
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 aa, bb and cc (the length of each string is between 1 and 2000). 

Output

For each test case, print ``Yes'', if cc is a combine string of aa and bb, otherwise print ``No''. 

Sample Input

abc
def
adebcf
abc
def
abecdf

Sample Output

Yes
No

 

两个字符串是否能组成另一个字符串,不要求连续,但字符顺序不变

 

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

const int maxn=4005;
int dp[maxn][maxn];
int main()
{
   string str1,str2,str0;
    while(cin>>str1>>str2>>str0){
        memset(dp,0,sizeof(dp));//dp[i][j]=0代表str1的 i个字符和 str2 的j个字符不能组成str0的i+j个字符
        int l0=str0.size(),l1=str1.size(),l2=str2.size();
        dp[0][0]=1;
        if(l0==l1+l2)
        {
            dp[0][0]=1;
            for(int i=0; i<=l1; i++)
            {
                for(int j=0; j<=l2; j++)
                {
                    if(str1[i]==str0[i+j]&&dp[i][j]==1) //str1的第i个字符  === str0的前i+j个字符
                    {
                        dp[i+1][j]=dp[i][j];  //str1的前i+1个字符加上 str2的前j个字符等于str0的i+j个字符
                    }
                    if(str2[j]==str0[i+j]&&dp[i][j]==1)//dp[i][j+1]的状态与dp[i][j]的状态一样
//                                                    也可以写作dp[i][j+1] | = dp[i][j]
                    {
                        dp[i][j+1]=dp[i][j];
                    }
                }
            }
            if(dp[l1][l2]==1)
            {
                cout<<"Yes"<<endl;
            }
            else
                cout<<"No"<<endl;
            }
            else {
                cout<<"No"<<endl;
            }

    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值