Zipper_DP
Description
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
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
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
【题意】给出三个字符串,求前两个是否包含在第三个中。
【思路】之前用过dfs,这次用dp,检验dp[len1][len2]是否为1;
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int dp[N][N];
int main()
{
int n;
char a[N],b[N],c[N];
int cas=;
while(~scanf("%d",&n))
{
cas++;
memset( dp,,sizeof(dp));
scanf("%s%s%s",a+,b+,c+);
int len1=strlen(a+);
int len2=strlen(b+);
int len3=strlen(c+);
for(int i=;i<=len1;i++)
{
if(a[i]==c[i]) dp[i][]=;
else break;
}
for(int j=;j<=len2;j++)
{
if(b[j]==c[j])
dp[][j]=;
else break;
}
for(int i=;i<=len1;i++)
{
for(int j=;j<=len2;j++)
{
if(c[i+j]==a[i]&&dp[i-][j])
dp[i][j]=;
if(c[i+j]==b[j]&&dp[i][j-])
dp[i][j]=;
}
}
printf("Data set %d: ",cas);
if(dp[len1][len2]) printf("yes\n");
else printf("no\n");
}
return ;
}
Zipper_DP的更多相关文章
随机推荐
- Java数据库移植框架
http://www.oschina.net/news/60591/flyway-3-2-released flyway 是一个敏捷工具,用于数据库的移植.采用 Java 开发,支持所有兼容 JDBC ...
- async 和 await 以及Action Func
C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...
- MongoDB相关资料
MongoDB的介绍及安装参考http://www.cnblogs.com/lipan/archive/2011/03/08/1966463.html 安装过程: 第一步:下载安装包:官方下载地址←单 ...
- C++ STL pair
没有找到priority_queue里存放pair不用typedef的方法...大概第一次觉得这个有用吧... 优先队列里和sort函数对pair 的默认排序是first从小到大,second从小到大 ...
- C#伪静态实现的方法
在asp.net开发网站的时候,我们经常会用到伪静态,好处是可以隐藏真实的路径,提高网站的安全性,在官网等展示网站希望对搜索引擎友好,提高搜索排名:或者在涉及到模板开发都会用到伪静态.下面讲解下平时用 ...
- [CSS]三层嵌套的滑动门
原理: 最外层放水平平铺的背景,第二层放左边,第三层放右边,注意这个做法背景图不能透明 结构: <div class="module-title"> <span ...
- TADOTable 用过滤事件 后 记录数据和 记录的内容
用 过滤事件,过滤后 ADOTbTrade.RecordCount 是总数, 但是,记录内容是 过滤后的 ADOTbTrade.First; while not ADOTbTrade.Eof do b ...
- Redhat6.x下如何制作虚拟机快照和镜像封装
一.虚拟机快照 1.确认你的物理机上的vg还有足够的剩余空间 [root@hacker ~]# vgs VG #PV #LV #SN Attr VSize VFree vg_ ...
- 如何在redhat下安装WineQQ
使用过redhat的朋友都知道在redhat下要使用聊天工具例如:腾讯QQ只能是用网页QQ,但网页QQ始终用得不尽人意,下面我将给大家介绍一种在redhat下安装WineQQ的方法,让你能在redha ...
- Repeater控件的分页效果
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" HorizontalAlign=" ...