Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5807    Accepted Submission(s): 2086
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
 

题意:输入三个字符串,前两个随意交错排列看能不能形成第三个

思路:本题有缺陷,就是测试数据弱爆了,所以直接暴力深搜加几个特解就能过,不过暴力深搜的就没给代码了,直接是平常深搜的代码,然后是DP的代码,解释在代码中

深搜:
#include <iostream>
#include <cstring>
using namespace std;
char s1[],s2[],s3[];
int l1,l2,visit[][];
bool dfs(int i,int j,int k)
{
if(k==l1+l2)return;//s3的下标等于s1加s2长度的时候就表示搜完了
if(visit[i][j])return;//这一步很重要,没了他就超时,深搜讲究的就是不重复搜同一个点
visit[i][j]=;
if(i<l1&&s1[i]==s3[k]&&dfs(i+,j,k+))return;//s1能和s3匹配就递归一次,返回值为1就接着返回,因为返回1表示的就是找到了对的路了
if(j<l2&&s2[j]==s3[k]&&dfs(i,j+,k+))return;//s2能和s3匹配也递归一次
return;
}
int main (void)
{
int n,i,j,k=;
cin>>n;
while(n--&&cin>>s1>>s2>>s3)
{
l1=strlen(s1);
l2=strlen(s2);
for(i=;i<;i++)//初始化标记数组
for(j=;j<;j++)
visit[i][j]=;
cout<<"Data set "<<k++<<": ";
if(dfs(,,))cout<<"yes"<<endl;//直接从三个字符串的第一个开始深搜
else cout<<"no"<<endl;
}
return;
}
DP://DP的想法就是DP[i][j]代表s1[i]和s2[j]这两个点同ss[i+j]匹配的当前状态
如匹配catrtee,不看其他杂乱的最终就得到这样个图(实际上还有些数字,因为可能是从中间开始匹配成功的)
    c a t
  0 1 2 0
t 0 0 3 0
r 0 0 4 5
e 0 0 0 6
e 0 0 0 7
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int l1,l2,l3;
char s1[],s2[],ss[];
int main (void)
{
int i,j,n,m=,dp[][];
scanf("%d",&n);
while(n--&&scanf("%s%s%s",s1,s2,ss))
{
memset(dp,,sizeof(dp));
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(ss);
if(ss[]==s1[])dp[][]=;//如果s1第一个和ss配上就标记初始状态“s1成功一个,s2成功0个”为1
        if(ss[]==s2[])dp[][]=;//同上
for(i=;i<=l1;i++)//有了初始状态之后就进行下面的递归
for(j=;j<=l2;j++)
{
if(i>&&s1[i-]==ss[i+j-])//s1没完,就用s1来匹配
dp[i][j]=max(dp[i][j],dp[i-][j]+);//取当前点的值与由前一个状态递推过来的值的最大值,因为有的从中间开始匹配成功的树值会干扰结果
if(j>&&s2[j-]==ss[i+j-])
dp[i][j]=max(dp[i][j],dp[i][j-]+);
}
printf("Data set %d: ",m++);
if(dp[l1][l2]==l3)
cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return;
}
 


HDU--杭电--1501--Zipper--深搜、DP都好的更多相关文章

  1. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  2. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  3. 深搜+DP剪枝 codevs 1047 邮票面值设计

    codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description ...

  4. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  5. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  6. HDU 2553 N皇后问题(深搜DFS)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

  8. 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)

    从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...

  9. 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...

  10. Zipper 杭电 1501

    Given three strings, you are to determine whether the third string can be formed by combining the ch ...

随机推荐

  1. PHP入门-摘要表格处理问题

    几天来学习下来.PHP和C/C++有太多的阶段似系.所以,简单的入门现在看来已经没有问题.然而,由于所选择的条目是一个高速书籍,难免有些粗糙知识.例如,下面的两个问题让我吃了一些损失. 1. 文件标签 ...

  2. 20140603 对error.c 用于分析源代码

    20140603 对error.c 用于分析源代码 继续看error.c该功能 买家现在将自己的代码和数据汇编例如,下面的:   1.#include <stdio.h>   2 #inc ...

  3. Web开发者的10个最好的云开发环境

    1. Cloud9 IDE cloud9 Cloud9是我最喜欢的一个最好的云开发环境,它可以让我在任何时间任何地点进行代码编写.运行和调试.Cloud9对Node.js 和 JavaScript代码 ...

  4. java中常见的单例模式详解

    很多求职者在面试过程中都被问到了单例模式,最常见的问题,比如,每种单例模式的区别是什么?哪些模式是线程安全的?你们项目里用的哪种单例模式?原来没有注意这个问题,回来赶紧打开项目查看了一下代码,才发现我 ...

  5. iOS开发--汉字转成没有声调也没有空格的拼音

     //汉字转成没有声调也没有空格的拼音- (NSString *)transformToPinYin:(NSString *)wordStr {    NSMutableString *mutable ...

  6. C++之对象组合

    #include<stdio.h>//初始化列表 提供了对成员变量初始化的方式//Constructor        class M      {       private:      ...

  7. Java中有关日期的一些常见运算与应用(Date,DateFormat,SimpleDateFormat)

    import java.text.DateFormat; import java.text.DateFormat; import java.text.SimpleDateFormat; import ...

  8. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )

    维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...

  9. 创建js对象和js类

    //第一种定义方式 var person=new Object(); //创建了一个对象. person.name="tom"; //使用person对象对调用name属性,它的值 ...

  10. win64位 apache2.4 php5.4 mysql5.6

    apache2.4 php5.4 mysql5.6 源文件下载 +以前的配置数据参考 链接:http://pan.baidu.com/s/1skfmGyT 密码:hqtp 比较好的参考资料 http: ...