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的更多相关文章
随机推荐
- SDWebImage源码刨根问底
前言: SDWebImage是iOS中一款处理图片的框架, 使用它提供的方法, 一句话就能让UIImageView,自动去加载并显示网络图片,将图片缓存到内存或磁盘缓存,正好有阅读开源项目的计划,于是 ...
- 细谈HTML5
回顾过了html接下来我们就来看看HTML5吧! HTML5手机应用的最大优势就是可以在网页上直接调试和修改.原先应用的开发人员可能需要花费非常大的力气才能达到HTML5的效果,不断地重复编码.调试和 ...
- PHP 页面编码声明方法详解(header或meta)
php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...
- BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息
建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size 于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了 /************* ...
- ANGULARJS 动态编译添加到dom中
在使用angularjs 时,希望通过动态构建angular模版,再通过angular进行展示. 使用 方法如下: <html ng-app="app"> <he ...
- js正则表达式和replace
javascript中replace与正则表达式 replace()最简单的算是能力就是简单的字符替换.示例代码如下: <script language="javascript&quo ...
- encodeURI
encodeURI("http://www.cnblogs.com/season-huang/some other thing"); //整个URL进行编码"http:/ ...
- swing LayoutManager 和多态
interface LayoutManager{ void show();}class FlowLayout implements LayoutManager{ public void show(){ ...
- matlab代码 图像处理源码
非常不错的找图像处理源码的地方,源码搜搜. http://www.codesoso.com/Category.aspx?CategoryId=56
- SharePoint 2013 Nintex Workflow 工作流帮助(一)
博客地址 http://blog.csdn.net/foxdave 接下来一段时间的内容中,我们基于SharePoint 2013来了解一下Nintex Workflow的具体内容. 之前的几篇由于之 ...