HDU-5707-Combine String2016CCPC女生专场DP

Posted 宣之于口

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-5707-Combine String2016CCPC女生专场DP相关的知识,希望对你有一定的参考价值。


HDU-5707-Combine String


Problem Description
Given three strings a, b and c, your mission is to check whether c is the combine string of a and b.
A string c is said to be the combine string of a and b if and only if c can be broken into two subsequences, when you read them as a string, one equals to a, and the other equals to b.
For example, adebcf‘‘ is a combine string ofabc” 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 a, b and c (the length of each string is between 1 and 2000).

Output
For each test case, print Yes‘‘, if c is a combine string of a and b, otherwise printNo”.

Sample Input
abc
def
adebcf
abc
def
abecdf

Sample Output
Yes
No

题目链接:HDU-5707

题目大意:这道题目看样例比较好懂。就是判断
1.a,b为c的字串
2.a串长度+b串长度相等
3.字串顺序不能反

题目思路:刚开始想贪心,后来考虑dp。

    dp[i][j]表示a串的长度为i,b串的长度为j,c串长度为i+j的时候是否可能拼成

初始化dp[0][0] = 1;

for (int i = 0; i <= a.size(); i++)  //a的数量
{
    for (int j = 0; j <= b.size(); j++)  //b的数量
    {
        if (c[i + j] == a[i])
        {
            dp[i + 1][j] |= dp[i][j];
        }
        if (c[i + j] == b[j])
        {
            dp[i][j + 1] |= dp[i][j];
        }
    }
}

//但是比赛的时候并没有写出来,赛后看了别人的代码,感觉是一样的。可能是某些细节原因。

以下是代码:

//
//  5707.cpp
//  2016-CCPC-GIRL
//
//  Created by pro on 16/7/3.
//  Copyright (c) 2016年 loy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
int dp[2005][2005];
int main()
{
    string a,b,c;
    while(cin >> a)
    {
        cin >> b >> c;
        if(a.size() + b.size() != c.size())  //注意判断长度
        {
            cout << "No\n";
            continue;
        }
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
        for (int i = 0; i <= a.size(); i++)  //a的数量
        {
            for (int j = 0; j <= b.size(); j++)  //b的数量
            {
                if (c[i + j] == a[i])
                {
                    dp[i + 1][j] |= dp[i][j];
                }
                if (c[i + j] == b[j])
                {
                    dp[i][j + 1] |= dp[i][j];
                }
            }
        }
        if (dp[a.size()][b.size()])cout << "Yes" << endl;
        else cout << "No\n";
    }
    return 0;
}

以上是关于HDU-5707-Combine String2016CCPC女生专场DP的主要内容,如果未能解决你的问题,请参考以下文章

glibc: Parsing a Template String 如何解析printf格式化

20-String和编码

1050 String Subtraction (20 分)

1050 String Subtraction (20 分)哈希 map

1050. String Subtraction (20)

pat 1050 String Subtraction(20 分)