HDU - 5707 Combine String(DP)


Combine String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2208    Accepted Submission(s): 623


Problem Description
Given three strings ab 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 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 ab 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 print ``No''.
 

Sample Input
abc def adebcf abc def abecdf
 

Sample Output
Yes No
 

Source
 

Recommend
liuyiding

題意: 有a、b、c三個串,當且僅當a、b都爲c的字串時輸出yes,否則輸出no

#include <bits/stdc++.h>

const int N = 2e3 + 10;
int dp[N][N];
char a[N], b[N], c[N];

int main()
{
    while(~scanf("%s %s %s",a , b, c))
    {
        memset(dp,0,sizeof(dp));
        int flag=0;
        int la = strlen(a), lb = strlen(b), lc = strlen(c);
        dp[0][0] = 1;
        for(int i = 0;i <= la; i++){
            for(int j = 0; j <= lb; j++){
                if(a[i] == c[i+j]){
                    dp[i+1][j] |= dp[i][j];
                }
                if(b[j]==c[i+j]){
                    dp[i][j+1] |= dp[i][j];
                }
            }
        }
        if(dp[la][lb] && lc == la + lb) printf("Yes\n");
        else printf("No\n");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章