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. BZOJ 1037: [ZJOI2008]生日聚会Party( dp )

    dp(i, j, a, b)表示选了i个男生, j个女生, 后缀中男生比女生多a(最多), 女生比男生多b(最多). dp(i+1, j, a+1, max(0, b-1)) += dp(i, j, ...

  2. R与数据分析旧笔记(三)不知道取什么题目

    连线图 > a=c(2,3,4,5,6) > b=c(4,7,8,9,12) > plot(a,b,type="l") 多条曲线效果 plot(rain$Toky ...

  3. springmvc结合freemarker,非自定义标签

    参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/ 上图: 目录层级: 启动后的访问地址:http://localhos ...

  4. PHP用户登录与注册页面

    PHP用户登录模块实现 项目包含的功能脚本: login.php//登录 reg.php//注册用户 user_add.php//注册校验脚本 user_login_check.php//登录校验脚本 ...

  5. xStream完美转换XML、JSON_java

    http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html

  6. eclipse 搭建Robotium环境--apk 环境搭建

    1.配置好android sdk ,java环境 2.重新签名apk文件 在用户目录下,会有一个.android的目录,把re-sign.jar放在该目录下.执行命令 java -jar re-sig ...

  7. C#中隐式操作CMD命令行窗口

    原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...

  8. php执行shell更新svn文件的方法

    vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...

  9. 在 Windows Azure 上部署预配置 Oracle VM

    Microsoft 和 Oracle 近期宣布建立战略合作伙伴关系,基于此,我们将通过 Windows Azure 镜像库推出多种常用的 Oracle 软件配置.即日起,客户可以在 Windows S ...

  10. [spring+springmvc+mybatis实践]学生社团管理系统

    一.简介 ssm框架为现在十分流行的mvc主流框架.mybatis负责与数据库交互,springmvc与spring完美适配,负责控制器和视图渲染.之前有初步学习过ssm框架,这次借学校里的web课设 ...