HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c
本题有两种解法 DP或者DFS
考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j个字符 值为0或者1 那么有d[i][j]=(d[i-1][j]&&a[i]==c[i+j])||(d[i][j-1]&&b[i]==c[i+j]) a,b的下标都是从1開始的 注意0的初始化
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 205;
char a[N], b[N], c[2 * N];
bool d[N][N]; int main()
{
int cas;
scanf ("%d", &cas);
for (int k = 1; k <= cas; ++k)
{
scanf ("%s%s%s", a + 1, b + 1, c + 1);
int la = strlen (a + 1), lb = strlen (b + 1), i = 1, j = 1;
memset (d, 0, sizeof (d)); while (a[i] == c[i] && i <= la)
d[i++][0] = true;
while (b[j] == c[j] && j <= lb)
d[0][j++] = true;
for (int i = 1; i <= la; ++i)
for (int j = 1; j <= lb; ++j)
d[i][j] = ( (d[i - 1][j] && a[i] == c[i + j]) || (d[i][j - 1] && b[j] == c[i + j])); printf ("Data set %d: ", k);
printf (d[la][lb] ? "yes\n" : "no\n");
}
return 0;
}
以下是dfs的代码 看是否能在ab中相应搜到c的每个字母就可
//DFS版
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 205;
char a[N], b[N], c[2 * N];
bool vis[N][N], ans;
void dfs (int i, int j, int k)
{
if (c[k] == '\0') ans = true;
if (ans || vis[i][j]) return ;
vis[i][j] = true;
if (a[i] == c[k]) dfs (i + 1, j, k + 1);
if (b[j] == c[k]) dfs (i, j + 1, k + 1);
}
int main()
{
int cas;
scanf ("%d", &cas);
for (int ca = 1; ca <= cas; ++ca)
{
ans = false;
memset (vis, 0, sizeof (vis));
scanf ("%s%s%s", a, b, c);
dfs (0, 0, 0);
printf ("Data set %d: ", ca);
printf (ans ? "yes\n" : "no\n");
}
return 0;
}
Zipper
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".
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.
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
版权声明:本文博主原创文章,博客,未经同意不得转载。
HDU 1501 Zipper(DP,DFS)的更多相关文章
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU(1572),最短路,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...
- 140. Word Break II (String; DP,DFS)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- Android client和服务器JSP互传中国
出于兼容性简化.传统中国等多国语言.推荐使用UTF-8编码. 首选.我们期待Android到底应该怎么办: 在发送前,应该对參数值要进行UTF-8编码,我写了一个static的 转换函数.在做发送动作 ...
- hdu4499 Cannon (DFS+回溯)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4499 Cannon ...
- xaml的margin和css的margin对比
css margin xaml margin 例子 1 css margin:10px 5px 15px 20px;上右下左 上外边距是 10px 右外边距是 5px 下外边距是 15px 左外边距是 ...
- 动态生成Zip
动态生成Zip文档 通过前面一篇烂文的介绍,大伙儿知道,ZipArchive类表示一个zip文档实例,除了用上一篇文章中所列的方法来读写zip文件外,还可以直接通过ZipArchive类,动态生成 ...
- 原生js实现 常见的jquery的功能
原生选择器 充分利用 bind(this)绑定 <div id="box"> <ul> <li >111 </li> <l ...
- 使用Java快速实现进度条(转)
基于有人问到怎样做进度条,下面给个简单的做法: 主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax. ...
- WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制
.NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制 项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口.以前没玩 ...
- hadoop-mapreduce在maptask执行分析
MapTask执行通过执行.run方法: 1.生成TaskAttemptContextImpl实例,此实例中的Configuration就是job本身. 2.得到用户定义的Mapper实现类,也就是m ...
- 具体评论ExpandableListView显示和查询模仿QQ组列表用户信息
在我们的项目开发过程,用户通常拥有的信息包,通过组来显示用户的信息,一时候通过一定的查询条件来显示查询后的相关用户信息.而且通过颜色选择器来设置列表信息的背景颜色. 当中借鉴xiaanming:htt ...
- sql查询第二大的记录(转)
问题: 数据库中人表有三个属性,用户(编号,姓名,身高),查询出该身高排名第二的高度.建表语句 create table users ( id ,) primary key, name ), heig ...