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. Android webViewj简单处理apk的下载链接

    最近在开发二维码扫一扫的功能,需要分多种情况处理外部的url.遇到一个问题是,一些程序包(比如一些android应用)的下载不好处理.如果不做任何处理的话,webView会打开一个空白页.比如这个链接 ...

  2. Android 常用动画小结

    1. 渐入动画 // Request the next activity transition (here starting a new one). startActivity(new Intent( ...

  3. 关于iPhone

    ---------------------- 美版有三个版本 A S V A版不能用电信卡,S不能发短信 据说还可能再次上锁 V版目前是大家认为最安全的版本 价格也是比A和S贵的 港版比V版唯一的好处 ...

  4. DOCTYPE声明的几种类型

    DOCTYPE声明的几种类型 DOCTYPE 声明决定着浏览器怎么去解析和渲染当前页面,所以对于页面来说是很重要的. HTML5时代,统一用 <!DOCTYPE html> 这样简单的方式 ...

  5. Win7下超级管理员创建普通权限任务

    已转至新的博客 http://www.raysoftware点击打开链接.cn/?p=49 项目中用到一个功能,Win7下超级管理员创建普通权限任务. 试了几种办法,例如获取资源管理器的Token,然 ...

  6. ThinkPHP导入Excel文件(使用PHPExcel)

    一. 主要知识点,用PHPExcel导入Excel数据经过这几天测试还是可以,xls,xlsx都可以获取Excel的数据.下载地址:http://phpexcel.codeplex.com/ O.开发 ...

  7. 【C/C++多线程编程之九】pthread读写锁

    多线程编程之读写锁      Pthread是 POSIX threads 的简称,是POSIX的线程标准.         pthread读写锁把对共享资源的訪问者分为读者和写者,读者仅仅对共享资源 ...

  8. struts2页面输出错误信息

    <package name="user" namespace="/user" extends="struts-default"> ...

  9. sql update 触发器 可获得被update的行的信息

    类型:转载   sql update 触发器 可获得被update的行的信息,需要的朋友可以参考下. 复制代码 代码如下: create trigger TgName on tb for update ...

  10. Unity UGUI在鼠标位置不同时 图片浮动效果

    /// <summary> /// 在鼠标位置不同时 图片浮动效果 /// </summary> public class TiltWindow : MonoBehaviour ...