意甲冠军  是否可以由串来推断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. c# 用正则表达式在指定的字符串中每隔指定个数的文字插入指定字符串

    public static string AddNewLine(string inString,int num,string addString="\r\n") { return ...

  2. 大约Android 了解权限管理

    如Android应用程序开发人员.为android权限机制一直觉得很奇怪.为什么要这个东西权限?为什么要AndroidManifest里面写的uses-permission 这样的事情?我一直搞不清楚 ...

  3. 图片切割工具---产生多个div切割图片 采用for和一的二维阵列设置背景位置

    照片库 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb21vZ2c=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. PL/SQL Developer ORA-12154: TNS: 无法解析指定的连接标识符

    底:         在这台机器(Win7 64位置  最后)设备Oracle 11g的client(已安装32位ORACLEclient.假设安装64位ORACLEclient的时候,在CMD命令中 ...

  5. C# - Dictionary join keys or join Values

    using System; using System.Collections.Generic; using System.Linq; using System.Text; public class P ...

  6. 第十二章——SQLServer统计信息(2)——非索引键上统计信息的影响

    原文:第十二章--SQLServer统计信息(2)--非索引键上统计信息的影响 前言: 索引对性能方面总是扮演着一个重要的角色,实际上,查询优化器首先检查谓词上的统计信息,然后才决定用什么索引.一般情 ...

  7. 【微信公众平台开发】百度周边搜索接口php封装

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl2YW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  8. QR代码简单

    QR代码(Quick Response Code, 高速响应码)属于二维矩阵码在一个.由DENSO(日本电装)公司开发,由JIS和ISO将其标准化. QR码分为两种模式:模式1.模式2.当中.模式1相 ...

  9. socket-详细分析No buffer space available(转)

    新年上班第一天,突然遇到一个socket连接No buffer space available的问题,导致接口大面积调用(webservice,httpclient)失败的问题,重启服务器后又恢复了正 ...

  10. Dojo Mobile制定学习用品

    Dojo Mobile开展 App技术开发QQ群:347072638 技术咨询.APP定制开发联系邮箱:messageloop@qq.com 时代在演变.技术在革新.无论你接受不接受. 初识Dojo ...