题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5707
题面:
Combine String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 395 Accepted Submission(s): 150
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 of ``abc'' and ``def''.
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 a , b and c (the length of each string is between 1 and 2000).
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 print ``No''.
Sample Input
abc def adebcf abc def abecdf
Sample Output
Yes No
Source
Recommend
liuyiding
解题:
还是太渣,想不到dp状态怎么表示。dp[i][j],表示前i个中有j个A串,是否符合条件,符合,则该值为1,不符合则为0。
代码:
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int l1,l2,l3;
string a,b,c;
bool dp[2005][2005];
int main()
{
int p1=0,p2=0,tmp;
bool flag;
while(cin>>a)
{
p1=p2=0;
flag=0;
cin>>b>>c;
l1=a.length();
l2=b.length();
l3=c.length();
memset(dp,0,sizeof(dp));
if(a[0]==c[0])
{
flag=1;
dp[1][1]=1;
}
if(b[0]==c[0])
{
flag=1;
dp[1][0]=1;
}
if(l1+l2!=l3)
flag=0;
if(flag)
{
for(int i=2;i<=l3;i++)
{
flag=0;
for(int j=0;j<i;j++)
{
if(dp[i-1][j])
{
if(a[j]==c[i-1])
{
dp[i][j+1]=1;
flag=1;
}
if(b[i-1-j]==c[i-1])
{
dp[i][j]=1;
flag=1;
}
}
}
if(!flag)break;
}
}
if(flag)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}