前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU - 1501 Zipper(dp&深搜)

HDU - 1501 Zipper(dp&深搜)

作者头像
天道Vax的时间宝藏
发布2021-08-11 10:42:05
1640
发布2021-08-11 10:42:05
举报
文章被收录于专栏:用户5305560的专栏

题目:HDU - 1501 Zipper

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

代码语言:javascript
复制
3
cat tree tcraete
cat tree catrtee
cat tree cttaree

Sample Output

代码语言:javascript
复制
Data set 1: yes
Data set 2: yes
Data set 3: no

题意理解:

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

思路(dp):

简单的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;

代码行(dp):

代码语言:javascript
复制
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<deque>
#include<cstdlib>
#include<fstream>
#include<map>
#include<iomanip>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<list>
typedef long long ll;
using namespace std;
string str1,str2,str;
int dp[440][440];
	
int main()
{

	int T;
	scanf("%d", &T);
	for(int k=0;k<T;k++)
	{
		cin>>str1>>str2>>str;

		int l1=str1.length(),
		    l2=str2.length(),
		    l=str.length();
		printf("Data set %d: ", k+1);
		if(l!=l1+l2)
		{
			cout<<"no"<<endl;
			continue;
		}
		memset(dp,0,sizeof(dp));
		dp[0][0]=1;
		for(int i=0;i<=l1;i++)
		{
			for(int j=0;j<=l2;j++)
			{
				if(i==0 && j==0) continue;
				if(i!=0 && dp[i-1][j]==1 && str1[i-1]==str[i+j-1])
				{
					dp[i][j]=1;
					continue;
				}
				if(j!=0 && dp[i][j-1]==1 && str2[j-1]==str[i+j-1])
				{
					dp[i][j]=1;
				}
			}
			
		}
		if(dp[l1][l2]==1) cout<<"yes"<<endl;
			else cout<<"no"<<endl;
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:HDU - 1501 Zipper
    • Input
      • Output
        • Sample Input
          • Sample Output
            • 题意理解:
              • 思路(dp):
                • 代码行(dp):
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档