博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HUD 1501 Zipper(记忆化 or DP)
阅读量:6136 次
发布时间:2019-06-21

本文共 3474 字,大约阅读时间需要 11 分钟。

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
 

Source
 
记忆化和DP均能够,通过两个串合成第三个串。顺序不变。
记忆化:
#include
#include
#include
#include
#include
typedef long long LL;using namespace std;const int maxn=220;char s1[maxn],s2[maxn],s[maxn<<1];int dp[maxn][maxn];int len,len1,len2;int t;int dfs(int i,int j,int k){ if(k==len) return 1; if(dp[i][j]) return 0; dp[i][j]=1; if(s1[i]==s[k]) { if(dfs(i+1,j,k+1)) return 1; } if(s2[j]==s[k]) { if(dfs(i,j+1,k+1)) return 1; } return 0;}int main(){ int l=0; scanf("%d",&t); while(t--) { scanf("%s%s%s",s1,s2,s); len1=strlen(s1); len2=strlen(s2); len=strlen(s); memset(dp,0,sizeof(dp)); if(dfs(0,0,0)) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0;}
DP:
#include
#include
#include
#include
#include
using namespace std;const int maxn=220;char s1[maxn],s2[maxn],s[maxn<<1];int dp[maxn][maxn];int main(){ int t; int l=0; scanf("%d",&t); getchar(); while(t--) { scanf("%s%s%s",s1+1,s2+1,s+1); int len1=strlen(s1+1); int len2=strlen(s2+1); int len=strlen(s+1); memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=0;i<=len1;i++) { for(int j=0;j<=len2;j++) { if(i==0&&j==0) continue; if(s[i+j]==s1[i]&&i&&dp[i-1][j]) dp[i][j]=1; if(s[i+j]==s2[j]&&j&&dp[i][j-1]) dp[i][j]=1; } } if(dp[len1][len2]) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0;}

转载地址:http://lskya.baihongyu.com/

你可能感兴趣的文章
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
直播视频流技术名词
查看>>
企业级java springboot b2bc商城系统开源源码二次开发-hystrix参数详解(八)
查看>>
java B2B2C 多租户电子商城系统- 整合企业架构的技术点
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>
数据库的这些性能优化,你做了吗?
查看>>
某大型网站迁移总结(完结)
查看>>
mysql的innodb中事务日志(redo log)ib_logfile
查看>>
部署SSL证书后,网页内容造成页面错误提示的处理办法
查看>>
MS SQLSERVER通用存储过程分页
查看>>
60.使用Azure AI 自定义视觉服务实现物品识别Demo
查看>>
Oracle 冷备份
查看>>