Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4884    Accepted Submission(s): 1742

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
 

其实还是LCS算法的思想。

opt[i][j] 表示 字符串2的前i个字符 和 字符串1 的前j个字符,可以匹配 字符串3 的最大个数。

例如 对于第一个case(省略第0行和第0列), 参看代码:

Data set 1:

2 3 3 3
2 4 5 5
2 4 6 7

状态方程:

opt[i][j] = max(opt[i-1][j] + (str1[ i-1 ] == str3[ opt[i-1][j] ]), opt[i][j-1] + (str2[ j-1 ] == str3[ opt[i][j-1] ]) );

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; int max(int a,int b){
return a > b ? a:b;
} int main(){
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
char str1[210],str2[210],str3[410];
int opt[410][410];
for(int c=1; c<=t; c++){
printf("Data set %d: ",c);
scanf("%s %s %s", str1,str2,str3);
int len1 = strlen(str1);
int len2 = strlen(str2);
int k = 0;
opt[0][0] = 0; for(int i=1; i<=len2; i++){
if(str2[i-1] == str3[i-1])
opt[0][i] = opt[0][i-1] + 1;
else
opt[0][i] = opt[0][i-1];
}
for( i=1; i<=len1; i++){
if(str1[i-1] == str3[i-1])
opt[i][0] = opt[i-1][0] + 1;
else
opt[i][0] = opt[i-1][0];
} for( i=1; i<=len1; i++){
for(int j=1; j<=len2; j++){ opt[i][j] = max(opt[i-1][j] + (str1[ i-1 ] == str3[ opt[i-1][j] ]), opt[i][j-1] + (str2[ j-1 ] == str3[ opt[i][j-1] ]) );
//cout << opt[i][j] << " ";
}
}
if(opt[len1][len2] == len1 + len2){
printf("yes\n");
}else
printf("no\n"); }
return 0;
}

HDU 1501 Zipper 动态规划经典的更多相关文章

  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. hdu 1501 Zipper

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

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

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

  5. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  6. HDU 1501 Zipper 字符串

    题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...

  7. HDU 1501 Zipper(DFS)

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

  8. hdu 1501 Zipper(DP)

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

  9. HDOJ 1501 Zipper 【简单DP】

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

随机推荐

  1. yii2.0 从控制器到视图的输出

    在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...

  2. 《Pointers On C》读书笔记(第一章 快速上手)

    1.C语言是一种自由格式的程序设计语言,没有规则要求我们必须如何书写语句.然而,如果我们在编写程序时能够遵守一些约定还是非常值得的,它可以使代码更加容易阅读和修改.另外,预处理命令有较为严格的规则. ...

  3. Linux ---> 监控JVM工具

    Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...

  4. 转载:Ajax及 GET、POST 区别

    转载:Ajax及 GET.POST 区别 收获: xhr.setRequestHeader(), xhr.getResponseHeader() 可以设置和获取请求头/响应头信息; new FormD ...

  5. .NET(C#):XmlArrayItem特性和XmlElement特性在序列化数组的差别

    原文http://www.cnblogs.com/mgen/archive/2011/12/04/2276238.html 比如这样一个类,我们用XmlArrayItem特性标明数组内出现的元素类型: ...

  6. 繁简转换OpenCC,autogb 和 autob5,iconv,python的jianfan包

    OpenCC OpenCC 是跨平台.多语言的开放中文转换库,除了基本的简繁转换功能外,用户还可以选择对不同用词习惯和异体字的处理方式. OpenCC 还提供方便的网页转换界面. OpenOffice ...

  7. linux内核源码阅读之facebook硬盘加速flashcache之四

    这一小节介绍一下flashcache读写入口和读写的基础实现. 首先,不管是模块还是程序,必须先找到入口,用户态代码会经常去先看main函数,内核看module_init,同样看IO流时候也要找到入口 ...

  8. vc++实现avi文件的操作

    为了对avi进行读写,微软提供了一套API,总共50个函数,他们的用途主要有两类,一个是avi文件的操作,一类是数据流streams的操作. 1.打开和关闭文件 AVIFileOpen ,AVIFil ...

  9. ceph启动脚本

    放在/etc/init.d/目录下,用法如下: root@u253:~# /etc/init.d/ceph === mon.a === usage: /etc/init.d/ceph [options ...

  10. SASS组件开发

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...