HDOJ 1501 Zipper 【DP】【DFS+剪枝】

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10886 Accepted Submission(s): 3925

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

题意

给出三个字符串 a, b, c 询问 字符串 C是不是由 字符串 A,B 顺序取出来组成的,也就是说,字符串C中取出两个子序列,会不会这两个子序列一个是A,一个是B 如果满足这个条件 是yes 反之 no

思路【DP】

假如字符串C的最后一个字符是字符串A 或者字符串B 的 那么 字符串C -1 的字符串 必然是由 字符串A -1 组成的字符串和字符串B 顺序取出 或者 是 字符串A 和字符串B - 1组成的字符串顺序取出,往前推就可以了

所以

DP[i][j] 分别表示 取字符串 A 的前 i 位,取字符串 B 的前 j 位

如果 DP[i - 1][j] == 1 && A[i - 1] == C[i + j - 1]

那么 DP[i][j] = 1

或者如果 DP[i][j - 1] == 1 && B[j - 1] == C[i + j - 1]

那么DP[i][j] = 1

AC代码

#include <bits/stdc++.h>  //DP
using namespace std;
const int maxn = 2 * 1e2 + 5;
int dp[maxn][maxn];
int main()
{
int t;
cin >> t;
int i, j, k;
for (k = 1; k <= t; k++)
{
string s[3];
int len[3];
for (i = 0; i < 3; i++)
{
cin >> s[i];
len[i] = s[i].size();
}
memset(dp, 0, sizeof(dp));
for (i = 0; i < len[0]; i++)
{
if (s[0][i] == s[2][i])
dp[i + 1][0] = 1;
else
break;
}
for (i = 0; i < len[1]; i++)
{
if (s[1][i] == s[2][i])
dp[0][i + 1] = 1;
else
break;
}
for (i = 1; i <= len[0]; i++)
{
for (j = 1; j <= len[1]; j++)
{
if (dp[i - 1][j] && s[0][i - 1] == s[2][i + j - 1])
dp[i][j] = 1;
if (dp[i][j - 1] && s[1][j - 1] == s[2][i + j - 1])
dp[i][j] = 1;
}
}
printf("Data set %d: ", k);
if (dp[len[0]][len[1]])
cout << "yes\n";
else
cout << "no\n";
}
}

思路【DFS】

如果 满足 a[x] == c[x + y]

那么我们就往 x + 1, y 去找

如果 满足 b[y] == c[x + y]

那么我们就往 x, y + 1 去找

AC代码

#include <bits/stdc++.h>    //DFS
using namespace std;
const int maxn = 2 * 1e2 + 5;
string a, b, c;
int len_a, len_b, len_c;
int ans;
int vis[maxn][maxn];
void dfs(int x, int y)
{
if (x + y == len_c)
{
ans = 1;
return ;
}
if (vis[x][y])
return ;
if (a[x] == c[x + y])
{
vis[x][y] = 1;
dfs(x + 1, y);
}
if (b[y] == c[x + y])
{
vis[x][y] = 1;
dfs(x, y + 1);
}
}
int main()
{
int t;
int k;
cin >> t;
for (k = 1; k <= t; k++)
{
cin >> a >> b >> c;
len_a = a.size(), len_b = b.size(), len_c = c.size();
printf("Data set %d: ", k);
ans = 0;
memset(vis, 0, sizeof(vis));
dfs(0, 0);
if (ans)
cout << "yes\n";
else
cout << "no\n";
}
}

HDOJ 1501 Zipper 【DP】【DFS+剪枝】的更多相关文章

  1. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  2. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  3. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  4. HDOJ(1010)DFS+剪枝

    Tempter of the Bone http://acm.hdu.edu.cn/showproblem.php?pid=1010 #include <stdio.h> #include ...

  5. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  6. 【题解】P3959 宝藏 - 状压dp / dfs剪枝

    P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的m  条道路和它们的长度. 小明决心亲自前往挖掘所有宝 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDOJ.1342 Lotto (DFS)

    Lotto [从零开始DFS(0)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tempter of ...

  9. HDOJ(HDU).1015 Safecracker (DFS)

    HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...

随机推荐

  1. 【vijos】1881 闪烁的繁星(线段树+特殊的技巧)

    https://vijos.org/p/1881 这场比赛太难了sad.所以我都没做.. 这题一开始我竟然不会sad(本来就不会),然后我继续yy..yy了好久,竟然yy了个什么可拆分的并查集?(sa ...

  2. 从零开始开发一个vue组件打包并发布到npm (把vue组件打包成一个可以直接引用的js文件)

    自己写的组件 有的也挺好的,为了方便以后用自己再用或者给别人用,把组件打包发布到npm是最好不过了,本次打包支持 支持正常的组件调用方式,也支持Vue.use, 也可以直接引用打包好的js文件, 配合 ...

  3. Java逍遥游记读书笔记<三>

    异常处理 如何判断一个方法中可能抛出异常 该方法中出现throw语句 该方法调用了其他已经带throws子句的方法. 如果方法中可能抛出异常,有两种处理方法: 1.若当前方法有能力处理异常,则用Try ...

  4. <转载> C++笔试、面试笔记

    这些东西有点烦,有点无聊.如果要去C++面试就看看吧.几年前网上搜索的.刚才看到,就整理一下,里面有些被我改了,感觉之前说的不对或不完善. 1.求下面函数的返回值( 微软) int func(x)  ...

  5. java关于Timer schedule执行定时任务 !!!!!!!!!

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等.对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Time ...

  6. 云计算和SDN中的开源交换机介绍以及使用

    之前关于SDN的开发工作都是在控制器层面上(以ryu为主),现在开始了新的工程项目,需要同时修改控制器和交换机的源码,如果后续项目需要,还可能需要加中间层——网络虚拟层,这部分的知识已经在前面读过了相 ...

  7. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  8. 如何修改Myeclipse的JSP模板

    先找到MyEclipse的安装目录, 再找到myeclipse/eclipse/plugins/com.genuitec.eclipse.wizards_5.1.0/templates/jsp (co ...

  9. 《挑战程序设计竞赛》2.4 数据结构-并查集 POJ1182 2236 1703 AOJ2170

    POJ1182 http://poj.org/problem?id=1182 题目 难得的中文题... 食物链 Time Limit: 1000MS Memory Limit: 10000K Tota ...

  10. 第09章—使用Lombok插件

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...