hdu-1501(dp或dfs)

Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11120    Accepted Submission(s): 4004


 

Problem Description

Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

For example, consider forming "tcraete" from "cat" and "tree":

String A: cat
String B: tree
String C: tcraete


As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":

String A: cat
String B: tree
String C: catrtee


Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".

 

Input

The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

 

 

Output

For each data set, print:

Data set n: yes

if the third string can be formed from the first two, or

Data set n: no

if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

 

Sample Input

 

3

cat tree tcraete

cat tree catrtee

cat tree cttaree

 

Sample Output

 

Data set 1: yes

Data set 2: yes

Data set 3: no

题意:给你三个字符串,问你前两个是否能组成第三个串,串中字符顺序不可改变,可拆分。

思路:简单的dp写法,用一个二维数组,第一维代表第一个字符串用了前几位,第二维代表第二个字符串用了前几位,若 dp[i][j] 可以组成 str[i+j]之前的所有字符,则为1,否为0。 
例子: abc def adebcf 
           dp为1的状态都有: dp[1][0] = 1;      取str1[0] 
                                          dp[1][1] = 1;      在dp[1][0]的基础上再取str2[0],以此类推 
                                          dp[1][2] = 1; 
                                          dp[2][2] = 1; 
                                          dp[3][2] = 1; 
                                          dp[3][3] = 1;

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
typedef unsigned long long ull;
typedef long long ll;

int f,lenc,dp[205][205];
char a[205],b[205],c[550];
int main()
{
	int cnt=1;
	int t;scanf("%d",&t);
	while(t--)
	{
		scanf("%s%s%s",a+1,b+1,c+1);
		int la=strlen(a+1);
		int lb=strlen(b+1);
		int lc=strlen(c+1);
		memset(dp,0,sizeof(dp));
		
		for(int i=1; i<=la; i++)
		{
			dp[i][0]=(a[i]==c[i]);
		}
		for(int i=1; i<=lb; i++)
		{
			dp[0][i]=(b[i]==c[i]);
		}
		for(int i=1; i<=la; i++)
		{
			for(int j=1; j<=lb; j++)
			{
				if(dp[i][j-1]&&b[j]==c[i+j])
				dp[i][j]=1;
				if(dp[i-1][j]&&a[i]==c[i+j])
				dp[i][j]=1;
			}
		}
		
		if(dp[la][lb])
			printf("Data set %d: yes\n",cnt++);
		else 
			printf("Data set %d: no\n",cnt++);
		
	}
    return 0;
}

dfs做法:都从第一位开始搜索

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
typedef unsigned long long ull;
typedef long long ll;

int f,lenc,vis[205][205];
char a[205],b[205],c[550];
void dfs(int x,int y,int z)
{
    if(z==lenc){f=1;return ;}
    if(vis[x][y])return ;
    vis[x][y]=1;
    if(a[x]==c[z])dfs(x+1,y,z+1);
    if(b[y]==c[z])dfs(x,y+1,z+1);
}

int main()
{
    int cnt=1;
    int t;scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%s%s%s",a,b,c);
        f=0;
        lenc=strlen(c);
        dfs(0,0,0);
        if(f)
            printf("Data set %d: yes\n",cnt++);
        else 
            printf("Data set %d: no\n",cnt++);
        
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/81155840