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,和此题很 ...
随机推荐
- two sets of Qt binaries into the same process的解决办法
突然出现了这样问题,吓死我,然后只是把原来编译好的app里面所有的东西删除再编译就好了. 如果删除后不行,可以试试后面的截图所说,反正我是没有试过的 Starting /Qtwork/build-te ...
- Windows8下通过IPv4地址访问Tomcat
最近在做Android开发,手机客户端需要通过IPv4地址访问电脑启动的Web应用服务. 在Windows 7不需要做什么设置,localhost,127.0.0.1或者192.168.0.100都可 ...
- Spring源码解析——如何阅读源码(转)
最近没什么实质性的工作,正好有点时间,就想学学别人的代码.也看过一点源码,算是有了点阅读的经验,于是下定决心看下spring这种大型的项目的源码,学学它的设计思想. 手码不易,转载请注明:xingoo ...
- asp.net 通过 Handler 导出数据至excel (让用户下载)
效果图: 代码: Export2Excel.ashx <%@ WebHandler Language="C#" CodeBehind="Export2Excel.a ...
- WebService推送数据,数据结构应该怎样定义?
存放在Session有一些弊端,不能实时更新.server压力增大等... 要求:将从BO拿回来的数据存放在UI Cache里面,数据库更新了就通过RemoveCallback "告诉&qu ...
- net core 中间件详解及项目实战
net core 中间件详解及项目实战 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的H ...
- C#、ASP.NET、WinForm - 实现邮件发送的功能
转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html 发送邮件所用的核心知识点 微软封装好的Mail ...
- 微软C#版SQLHelper.cs类
转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247 using System; using System.Data; u ...
- MVC控制器里面使用dynamic和ExpandoObject
MVC控制器里面使用dynamic和ExpandoObject 在很多时候,我们在数据库里面定义表字段和实际在页面中展示的内容,往往是不太匹配的,页面数据可能是多个表数据的综合体,因此除了我们在表设计 ...
- Linux注销在线用户
与Windows系统类似,Linux系统上也有注销在线用户的方法,我们可以使用pkill命令,详细的步骤如下: . 首先使用w或who命令查看在线用户,确定用户所在TTY [root@iavp232 ...