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. SFTP上传下载(C#)

    sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Re ...

  2. cocos2d-x游戏开发系列教程-坦克大战游戏之敌方坦克AI的编写

    在上篇我们完成了子弹和地图碰撞的检测,在这篇我们将完成敌方坦克AI的编写. 具体思路是屏幕中保持有四个敌方坦克,然后坦克随机方向运动,并且子弹消失后1秒发射一次 1.我们新建一个敌方坦克的AI类来控制 ...

  3. 初探 插头DP

    因为这题,气得我火冒三丈! 这数据是不是有问题啊!我用cin代替scanf后居然就AC了(本来一直卡在Test 18)!导致我调(对)试(排)了一个小时!! UPD:后来细细想想,会不会是因为scan ...

  4. JAVA_2Lesson

    package test; public class abc { public static void main(String[] arg) { int[][] xx=new int[3][]; xx ...

  5. []: secureCRT连接ubuntu问题- The remote system refused the connection

    secureCRT连接ubuntu问题- The remote system refused the connection http://jxyang.iteye.com/blog/1484915 解 ...

  6. UML04-顺序图

    1.顺序图由哪几部分组成? 2.下面列出了打印文件时的工作流: 用户通过计算机指定要打印的文件. 打印服务器根据打印机是否空闲,操作打印机打印文件. 如果打印机空闲,则打印机打印文件: 如果打印机忙, ...

  7. log4net使用经验总结

    下面介绍几条我认为比较好的经验,让我们更好的运用log4net 1.web程序时不把log4net的配置文件放在web.config中 原因:一个项目随着需求的变更,配置字节会特别多,不便查阅及维护. ...

  8. encode_utf8 把字符编码成字节 微信例子

    ##µ¼Èë encode_json decode_json use JSON qw/encode_json decode_json/; print "1111111111111111-\$ ...

  9. PHP $_SERVER

    $_SERVER 是一个包括了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web server创建.不能保证每一个 ...

  10. c语言,gdb

    Get gdb call stack http://blog.csdn.net/zoufeiyy/article/details/1490241 Debugging with GDB - Examin ...