HUD 1501 Zipper(记忆化 or DP)
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".
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.
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.
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
Data set 1: yes
Data set 2: yes
Data set 3: no
#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)的更多相关文章
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- HDU1978How Many Ways 记忆化dfs+dp
/*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- 记忆化搜索 dp学习~2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331 Function Run Fun Time Limit: 2000/1000 MS (Java/ ...
- [P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm (记忆化搜索/DP?,Tarjan?)
第一看还以为是水题 随便打了一个bfs只有40分…… 然后开始颓废 #include<bits/stdc++.h> #define N 100005 using namespace std ...
- 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】
Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...
- hdu1331&&hdu1579记忆化搜索(DP+DFS)
这两题是一模一样的``` 题意:给了一系列递推关系,但是由于这些递推很复杂,所以递推起来要花费很长的时间,所以我要编程序在有限的时间内输出答案. w(a, b, c): 如果a,b,c中有一个值小于等 ...
- 1415. [NOI2005]聪聪和可可【记忆化搜索DP】
Description Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点 ...
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
随机推荐
- SuperSocket源码解析之启动过程
一 简介 这里主要说明从配置系统引导启动SuperScoekt作为应用程序,且以控制台程序方式启动 二 启动过程 2.1 配置解析 从读取配置文件开始,直接拿到一个SocketServiceConfi ...
- Qt调用DLL
声明: 事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的.并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下: ...
- 支付平台程序,支付程序,网络pos程序,api接口程序,锋锐支付平台程序开发领导者!
支付平台程序,支付程序,网络pos程序,api接口程序,锋锐支付平台程序开发领导者! 锋锐支付平台程序(www.100freenet.com)隶属于盐城市沐良商贸有限公司(沈阳杰速网络科技有限公司旗下 ...
- hdu4722之简单数位dp
Good Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 黑马day16 jquery&层次选择器
假设想通过DOM元素之间的层次关系来获取特定元素,比如后代元素,子元素,相邻元素,兄弟元素等,则须要使用层次选择器. 1 .ancestor descendant 使用方法: $("form ...
- PHP - 创建一个类
/* * 类的实现 */ //声明一个类 class Person { //私有字段 private $name; private $sex; private $age; //构造函数 functio ...
- CentOS的ssh sftp配置及权限设置(流程相当完整)(关闭了SElinux才能上传了)
从技术角度来分析,几个要求: 1.从安全方面看,sftp会更安全一点 2.线上服务器提供在线服务,对用户需要控制,只能让用户在自己的home目录下活动 3.用户只能使用sftp,不能ssh到机器进行操 ...
- static在C和C++中的用法和区别
static主要有三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数据成员/成员函数 前两种C和C++都有,第三种仅在C++中有,下面分别作以下介绍: 一.局部静态变量 在C/C+ ...
- 基于visual Studio2013解决C语言竞赛题之1027 YN
题目 解决代码及点评 /* 计算Yn的值,直到|Yn - Yn-1|<10-6为止,并打印出此时共作了多少次COS计算. 提示:Yn+1=COS(Yn),故本 ...
- Xcode4.6 自制iOS可用的 Framework
First of all:新建一个空白project. File->New->Project 然后新建两个文件 File->New->File 如图 然后选择targets ...