码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-5707-Combine String【2016CCPC女生专场】【DP】

时间:2016-07-03 19:13:04      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:


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 String【2016CCPC女生专场】【DP】

标签:

原文地址:http://blog.csdn.net/loy_184548/article/details/51814884

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!