HDU--杭电--1501--Zipper--深搜、DP都好
Zipper
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
题意:输入三个字符串,前两个随意交错排列看能不能形成第三个
思路:本题有缺陷,就是测试数据弱爆了,所以直接暴力深搜加几个特解就能过,不过暴力深搜的就没给代码了,直接是平常深搜的代码,然后是DP的代码,解释在代码中
深搜:
#include <iostream>
#include <cstring>
using namespace std;
char s1[],s2[],s3[];
int l1,l2,visit[][];
bool dfs(int i,int j,int k)
{
if(k==l1+l2)return;//s3的下标等于s1加s2长度的时候就表示搜完了
if(visit[i][j])return;//这一步很重要,没了他就超时,深搜讲究的就是不重复搜同一个点
visit[i][j]=;
if(i<l1&&s1[i]==s3[k]&&dfs(i+,j,k+))return;//s1能和s3匹配就递归一次,返回值为1就接着返回,因为返回1表示的就是找到了对的路了
if(j<l2&&s2[j]==s3[k]&&dfs(i,j+,k+))return;//s2能和s3匹配也递归一次
return;
}
int main (void)
{
int n,i,j,k=;
cin>>n;
while(n--&&cin>>s1>>s2>>s3)
{
l1=strlen(s1);
l2=strlen(s2);
for(i=;i<;i++)//初始化标记数组
for(j=;j<;j++)
visit[i][j]=;
cout<<"Data set "<<k++<<": ";
if(dfs(,,))cout<<"yes"<<endl;//直接从三个字符串的第一个开始深搜
else cout<<"no"<<endl;
}
return;
}
DP://DP的想法就是DP[i][j]代表s1[i]和s2[j]这两个点同ss[i+j]匹配的当前状态
如匹配catrtee,不看其他杂乱的最终就得到这样个图(实际上还有些数字,因为可能是从中间开始匹配成功的)
c a t
0 1 2 0
t 0 0 3 0
r 0 0 4 5
e 0 0 0 6
e 0 0 0 7
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int l1,l2,l3;
char s1[],s2[],ss[];
int main (void)
{
int i,j,n,m=,dp[][];
scanf("%d",&n);
while(n--&&scanf("%s%s%s",s1,s2,ss))
{
memset(dp,,sizeof(dp));
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(ss);
if(ss[]==s1[])dp[][]=;//如果s1第一个和ss配上就标记初始状态“s1成功一个,s2成功0个”为1
if(ss[]==s2[])dp[][]=;//同上
for(i=;i<=l1;i++)//有了初始状态之后就进行下面的递归
for(j=;j<=l2;j++)
{
if(i>&&s1[i-]==ss[i+j-])//s1没完,就用s1来匹配
dp[i][j]=max(dp[i][j],dp[i-][j]+);//取当前点的值与由前一个状态递推过来的值的最大值,因为有的从中间开始匹配成功的树值会干扰结果
if(j>&&s2[j-]==ss[i+j-])
dp[i][j]=max(dp[i][j],dp[i][j-]+);
}
printf("Data set %d: ",m++);
if(dp[l1][l2]==l3)
cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return;
}
HDU--杭电--1501--Zipper--深搜、DP都好的更多相关文章
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 深搜+DP剪枝 codevs 1047 邮票面值设计
codevs 1047 邮票面值设计 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】
pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- HDU 2553 N皇后问题(深搜DFS)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)
呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...
- 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)
从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...
- 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...
- Zipper 杭电 1501
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
随机推荐
- Aspx 页面生命周期
ASP.NET 页运行时,此页将经历一个生命周期,在生命周期中将执行一系列处理步骤.这些步骤包括初始化.实例化控件.还原和维护状态.运行事件处理程序代码以及进行 呈现.了解页的生命周期非常重要,这样就 ...
- Java多线程之synchronized(一)
在上节中已经说过了“非线程安全”是如何出现的,链接如下:http://www.cnblogs.com/chentong/p/5650137.html,那么怎么解决“非线程安全”问题呢,只需要在两个线程 ...
- 【JAVA】修改项目包名
从最后一层开始修改,一步步往上递增修改.
- React Native-目前最火的前端技术?
做为一名产品经理,你是否遇到过这样的窘境,“帮我把字体调成 16号呗,颜色变成 #FFFF00FF,老大说这里最好改一下”,作为一名 app 的开发只能无奈但心里窃喜的告诉你,“只能等下个版本了,必须 ...
- URAL 1203 Scientific Conference dp?贪心
题目:click here 分明就是贪心怎么会在dp的专题 #include <bits/stdc++.h> using namespace std; typedef unsigned l ...
- python之filter过滤器
Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是 ...
- uml笔记
把进度放在好了: 活动图与业务流程 对业务流程支持的主要图形就是活动图,活动图的主要目的在陈述活动与活动之间流程控制的转移.
- Linux 环境下 fork 函数和 exec 函数族的使用
前言 接触 Linux 已经有几个月了,以前在网上看各路大神均表示 Windows 是最烂的开发平台,我总是不以为然,但是经过这段时间琢磨,确实觉得 Linux 开发给我带来不少的便利.下面总结一下学 ...
- PDO获取数据的方法fetch()、fetchAll()、setFetchMode()、bindColumn()
PDO的数据获取方法与其他数据库扩展都非常类似,只要成功执行SELECT查询,都会有结果集对象产生.不管是使用PDO对象中的query()方法,还是使用prepare()和execute()等方法结合 ...
- [LeetCode]题解(python):014-Longest Common Prefix
题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. ...