Zipper
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17585   Accepted: 6253

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
 
  DP一直以来都没怎么看,觉得很难,也只会抄抄模板和处理一些简单的背包。于是比赛出现了这道比较简单的题还是不会,要开始去学学DP了。

  题意:给出3个字符串,分别为A串和B串还有C串,题目保证A的长度+B的长度=C的长度,C里面包含着A和B的字符,然后判断A和B在C里面的字符顺序还是不是和原来的A和B保持原样。

  做法:建立一个DP数组dp[i][j],第一维i表示使用了A的前i个字符,第二维j表示使用了B的前j个字符,然后赋予1或者0判断是否可以组成。如果此时c[i+j]==a[i]&&dp[i-1][j]则表示可以使用a[i]这个字符来组成c[i+j],所以dp[i][j]=1。同理,如果c[i+j]==b[j]&&dp[i][j-1]则表示可以使用b[j]来组成c[i][j],所以dp[i][j]=1。(我这里直接让dp[i][j]的i和j都向后偏移了1)。状态转移方程:dp[i][j]=(c[i+j]==a[i]&&dp[i-1][j])||(c[i+j]==b[i]&&dp[i][j-1])。

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
#define MAXN 1010
#define inf 1000000
typedef pair<int,int> P;
int dp[MAXN][MAXN]; int main()
{
int t;
cin>>t;
string a,b,c;
for(int cas=;cas<=t;cas++){
cin>>a>>b>>c;
memset(dp,,sizeof(dp));
int la=a.size(),lb=b.size(),lc=c.size();
int i,j,k;
dp[][]=;
for(i=;i<=la;i++){
for(j=;j<=lb;j++){
if(i<la&&a[i]==c[i+j]&&dp[i][j]){
dp[i+][j]=;
}
if(j<lb&&b[j]==c[i+j]&&dp[i][j]){
dp[i][j+]=;
}
}
}
bool flag=true;
if(!dp[la][lb]) flag=false; printf("Data set %d: ",cas);
if(flag) printf("yes\n");
else printf("no\n");
} return ;
}
/*
如果串a的前i个字符和串b的前j个字符组成串c
并且满足未加入第i或j个字符前dp[i][j]可行
那么dp[i][j]=1
*/

2016-05-15

POJ 2192 :Zipper(DP)的更多相关文章

  1. POJ 1260:Pearls(DP)

    http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8 ...

  2. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  3. POJ 3858 Hurry Plotter(DP)

    Description A plotter is a vector graphics printing device that connects to a computer to print grap ...

  4. Codeforces Gym101341K:Competitions(DP)

    http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...

  5. HDU 5791:Two(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description   Alice gets two sequences A ...

  6. POJ - 2385 Apple Catching (dp)

    题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...

  7. POJ 1191 棋盘分割(DP)

    题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...

  8. 咸鱼的ACM之路:动态规划(DP)学习记录

    按挑战程序设计竞赛介绍的顺序记录一遍学习DP的过程. 1. 01背包问题 问题如下: 有N个物品,每个物品(N[i])都有一定的体积(W[i]),和一定的价值(V[i]) 现在给定一个背包,背包的容量 ...

  9. hdu 1501 Zipper(DP)

    题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...

随机推荐

  1. MAC下Android的Eclipse开发环境的搭建

    一.Eclipse的下载 到网站:http://www.eclipse.org/downloads/ 上,由于我们是用Java开发的所以步骤如下: 1.  找到"Eclipse IDE fo ...

  2. python之map、filter、reduce、lambda函数

    map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...

  3. box2dweb之关节joint(连接器)

    1 概述 前篇基础文章看完后基本上就应该对box2dweb能上手了,下面来介绍一下box2dweb非常重要的一个概念,关节(joint)也有叫连接器的,总之是一个意思.下面是关节详细的类库说明: BO ...

  4. iOS 对UIButton的imageView和titleLabel进行重新布局

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  5. iOS使用代码截图

    /** * 截图代码 * * @param view 需要截图的view * @param rect 需要截取的区域 * * @return 返回截取的对象 */ + (UIImage *)viewS ...

  6. [Android Tips] 3. Launch CallLog Activity

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(android.provider.CallLog.Calls.CONTEN ...

  7. Struts2上传图片时报404错误

    可能是struts配置文件中定义的拦截器导致的,后缀拦截导致,将该拦截器去掉,在action类里判断后缀 public String upload()throws Exception{ ActionC ...

  8. linux:什么是linux

    1>.linux是一套作业系统(linux就是核心与呼叫这两层),每一种作业系统都是在他专门的硬体机器上面运行的:linux是一个Open Source的作业系统,具有可移植性 2>.li ...

  9. SQL百分比

    百分比=cast(cast(count(ProvinceName)*100./ (select count(Id) from  dbo.TopicCurrent   where boardId=316 ...

  10. pg_rewind 介绍

    pg_rewind—使一个PostgreSQL数据目录与另一个数据目录(该目录从第一个PostgreSQL数据目录创建而来)一致. 描述 pg_rewind是一个在集群的时间线参数偏离之后,用于使一个 ...