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
 
Source
 
记忆化和DP均能够,通过两个串合成第三个串。顺序不变。
记忆化:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=220;
char s1[maxn],s2[maxn],s[maxn<<1];
int dp[maxn][maxn];
int len,len1,len2;
int t;
int dfs(int i,int j,int k)
{
if(k==len)
return 1;
if(dp[i][j])
return 0;
dp[i][j]=1;
if(s1[i]==s[k])
{
if(dfs(i+1,j,k+1))
return 1;
}
if(s2[j]==s[k])
{
if(dfs(i,j+1,k+1))
return 1;
}
return 0;
}
int main()
{
int l=0;
scanf("%d",&t);
while(t--)
{
scanf("%s%s%s",s1,s2,s);
len1=strlen(s1);
len2=strlen(s2);
len=strlen(s);
memset(dp,0,sizeof(dp));
if(dfs(0,0,0))
printf("Data set %d: yes\n",++l);
else
printf("Data set %d: no\n",++l);
}
return 0;
}

DP:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int maxn=220;
char s1[maxn],s2[maxn],s[maxn<<1];
int dp[maxn][maxn]; int main()
{
int t;
int l=0;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s%s%s",s1+1,s2+1,s+1);
int len1=strlen(s1+1);
int len2=strlen(s2+1);
int len=strlen(s+1);
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<=len1;i++)
{
for(int j=0;j<=len2;j++)
{
if(i==0&&j==0)
continue;
if(s[i+j]==s1[i]&&i&&dp[i-1][j])
dp[i][j]=1;
if(s[i+j]==s2[j]&&j&&dp[i][j-1])
dp[i][j]=1;
}
}
if(dp[len1][len2])
printf("Data set %d: yes\n",++l);
else
printf("Data set %d: no\n",++l);
}
return 0;
}

HUD 1501 Zipper(记忆化 or DP)的更多相关文章

  1. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  2. HDU1978How Many Ways 记忆化dfs+dp

    /*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...

  3. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  4. 记忆化搜索 dp学习~2

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331 Function Run Fun Time Limit: 2000/1000 MS (Java/ ...

  5. [P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm (记忆化搜索/DP?,Tarjan?)

    第一看还以为是水题 随便打了一个bfs只有40分…… 然后开始颓废 #include<bits/stdc++.h> #define N 100005 using namespace std ...

  6. 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】

    Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...

  7. hdu1331&&hdu1579记忆化搜索(DP+DFS)

    这两题是一模一样的``` 题意:给了一系列递推关系,但是由于这些递推很复杂,所以递推起来要花费很长的时间,所以我要编程序在有限的时间内输出答案. w(a, b, c): 如果a,b,c中有一个值小于等 ...

  8. 1415. [NOI2005]聪聪和可可【记忆化搜索DP】

    Description Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点 ...

  9. Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)

    题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...

随机推荐

  1. WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[元数据描述篇]

    原文:WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[元数据描述篇] 在[WS标准篇]中我花了很大的篇幅介绍了WS-MEX以及与它相关的WS规范:WS-Policy.WS-Tra ...

  2. Bootstrap技术: 模式对话框的使用

    一.概述 说到模式对话框,大家肯定都会想到windows下GUI程序,在gui程序中,有大量的对话框. 在web程序中,随着页面交互式功能的增多,有很多场景下也会用到对话框.在html原生的支持下,有 ...

  3. Mockito简介(转)

    Mockito 是目前 java 单测中使用比较流行的 mock 工具.其他还有 EasyMock,JMock,MockCreator,Mockrunner,MockMaker 及 PowerMock ...

  4. HDU4707:Pet(DFS)

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  5. 清华集训2014 day1 task3 奇数国

    题目 题目看起来好像很难的样子!其实不然,这是最简单的一道题. 算法 首先要注意的是: \(number \cdot x + product \cdot y = 1\) ,那么我们称\(number\ ...

  6. freemarker自己定义标签报错(三)

    freemarker自己定义标签 1.错误描写叙述 freemarker.core.ParseException: Encountered " " at line 14, colu ...

  7. Swift - 判端网络连接状态,连接类型(3G还是Wifi)

    IJReachability是一个使用Swift写的第三方网络检测类.可以测试网络是否连接,并支持3G和Wifi的检测. 使用样例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. CF 8D Two Friends (三分+二分)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :有三个点,p0,p1,p2.有两个人ali ...

  9. IOS开发之UIView的基本使用

    一.视图 1. iphone手机上的窗口就是UIWindow类的一个实例(1个手机应用只有一个UIWindow). 2.UIView类用于实现视图. UIView提供了方法来添加和删除子视图.一个视图 ...

  10. 使用boost io_service时,需要注意的东西

    boost::asio 在创建io_service时,可以指定线程数,如果没有指定,默认是一个线程,也就是io_service run的那个线程,如果没有任务运行,该线程会退出. 如果在创建的时候指定 ...