意甲冠军  是否可以由串来推断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

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
 

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU 1501 Zipper(DP,DFS)的更多相关文章

  1. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  2. hdu 1501 Zipper dfs

    题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...

  3. (step4.3.5)hdu 1501(Zipper——DFS)

    题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...

  4. hdu 1501 Zipper(DP)

    题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...

  5. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  6. hdu 1501 Zipper

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...

  7. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

  9. 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 ...

随机推荐

  1. 编写ruby扩展库

    # Loads mkmf which is used to make makefiles for Ruby extensions require 'mkmf' # Give it a name ext ...

  2. Jafka来源分析——Processor

    Jafka Acceptor接受client而建立后的连接请求,Acceptor会将Socket连接交给Processor进行处理.Processor通过下面的处理步骤进行client请求的处理: 1 ...

  3. 玩转Web之Jsp(二)-----jsp中怎么使用CKEditor

    在BBS项目或其他一些项目中,我们会发现别人写的一些文本域非常漂亮,而且有多种功能,这是怎么做到的呢?其实通过在jsp文件中引用在线编辑器即可,这里以CKEditor为例. 首先下载CKEditor: ...

  4. 处理FTP上传成功推理

    #登录FTPserver获取指定文件 #$1:server住址 #$2:帐户 #$3:password #$4:文件路径名 #$5:本地文件路径名 #$6:本地文件名 #比量ftp上传结果: #获取上 ...

  5. Linux-2.6.32内核编译流量计数器nfacct

    最近一直想看到一本书<一个Jiegeng华>.而技术不依赖书.但是,这并不表示我IT技术没有兴趣.事实证明,,当我无法理解的沧桑.肮脏的领导者无法理解的心理.自我可惜无法理解它处处感受到脏 ...

  6. HDU 3874 离线段树

    在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...

  7. 使用Visual Studio 2010写Data Url生成工具C#版本

    声明:本文系本人按照真实经历原创.未经许可,谢绝转载. 此文百度经验版本号:怎样用Visual Studio 2010打造Data Url生成工具 源代码下载:用Visual Studio 2010编 ...

  8. iis7、iis8配置备份还原

    原文 iis7.iis8配置备份还原 方法1: 1.打开我们的IIS管理器,在功能视图里找到“共享的配置”这个功能然后双击进入. 2.进入“共享的配置”后单机右上方的“导出配置”选项,选择导出配置文件 ...

  9. Visual FoxPro 6.0~9.0解决方案和实例文档和CD写入原件下载

    自从微软宣布开发冻结Visual FoxPro之后,这样的图书出版已经成为一个问题,但仍有不少VFP小贴士.处处留心此8历史书.在此提供写作的原稿.它看起来非常舒服比扫描版淘宝.下载链接:http:/ ...

  10. eclipse字母大写和小写转换的快捷键

    大写转换小写 ctrl+shift+y        小写转换大写 ctrl+shift+x