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 ...
随机推荐
- CSS中的repeat
Repeat-x是横向铺满,就是图片会横向重复,直到铺满. Repeat-y是纵向铺满,就是让图片纵向重复,直到铺满. 如果不想让重复,就直接为:no-repeat.
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...
- ibatis缓存配置
一.sqlmapconfig.xml <sqlMapConfig> <settings useStatementNamespaces="true" cacheM ...
- 分支-15. 日K蜡烛图(15)
#include<iostream> using namespace std; int main(){ float o,h,l,c; while(cin>>o>>h ...
- Visual Studio shortcut keys
VS2010 快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CT ...
- 转: seajs手册与文档之 -- 模块标识
目录 模块标识 相对标识 顶级标识 普通路径 文件后缀的提示 模块标识 模块标识是一个字符串,用来标识模块.在 require. require.async 等加载函数中,第一个参数都是模块标识.de ...
- 设计模式总结6--适配器模式 adapter pattern
适配器模式将一个类的接口,转化成客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间 public interface Sheep{ public void run(); public voi ...
- 解题报告 HDU1087 Super Jumping! Jumping! Jumping!
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 复习知识点:UITableView和UICollectionView的常用属性
UITableView UICollectionView //UICollectionViewLayout //UICollectionViewLayout决定了UICollectionView如何 ...
- ios inHouse 公布应用
一.明白几个概念 1.企业版IDP: 即iOS Development Enterprise Program.注意是$299/Year那种.并非$99/Year的那种 2.In House:是指企业内 ...