HDU 1501 Zipper 动态规划经典
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4884 Accepted Submission(s): 1742
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".
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.
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.
cat tree tcraete
cat tree catrtee
cat tree cttaree
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 动态规划经典的更多相关文章
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- HDU 1501 Zipper 字符串
题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
随机推荐
- C++/C#结构体转化-传string给C++
此例是把C#结构传给C++ C++: typedef struct VidyoClientInEventGroupChat_ { /*! Message (contents) to be sent t ...
- [C#参考]UI和线程(一)
Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程. 什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包 ...
- Mongodb安装和基本命令
本人是在Centos中安装的mongodb 1.下载mongodb curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2. ...
- PHP无法获取Referer问题排查
测试结果: 同一个页面,2次打开,第一次能获取到Referer第二次获取不到,很好奇原因所在. test1.php代码是: <?php echo '测试来源:直接载入页面<br/>' ...
- 用SCMD2.0.8.0汉化版制作OB地图简易教程
[综合] [复制链接] Fenix_king 153 主题 0 好友 1万 积分 金星 该用户从未签到 星币 6392 水晶 0 星望 22 精华 0 发消息 电梯直达 楼主 ...
- 监控Informix-Url
jdbc:informix-sqli://[{ip-address|host-name}:{port-number|service-name}][/dbname]: INFORMIXSERVER=se ...
- xmemcached的time out
最近维护线上发现不停有java.util.concurrent.TimeoutException: Timed out(200) waiting for operation的问题,排查程序.配置文件的 ...
- Qt程序打包成exe可执行文件图文教程(图文并茂,且用到了filepack)
很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
- HDU 3507 Print Article(CDQ分治+分治DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...