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. 项目管理利器——Maven

    假设公司要开发一个新的Web项目,使用目前流行的struts2.spring.MyBatis进行新项目开发.那么接下来首先要进行的工作就是各个框架的jar包的下载.大家通常的做法是先到struts2的 ...

  2. php扩展mssql.so连接sqlserver2008

    1.安装配置freetds  wget http://mirrors.xmu.edu.cn/ubuntu/archive/pool/main/f/freetds/freetds_0.82.orig.t ...

  3. 浅谈EasyUI---C#三层架构---

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  4. SQL Server Index详解

    最近在进行数据库调优,对索引的使用和其内部的运转一知半解.在园子里看到一篇相关文章非常好.留下印记以便日常查找. http://www.cnblogs.com/xwdreamer/archive/20 ...

  5. c++ template怎么使用及注意事项

    c++ 中的template和c#的什么有点相似? 先看下定义方式: template <typename|class T> T myFunction(T param1,T param2. ...

  6. Ruby On Rails 环境搭建MySQL数据库连接

    1.  安装wamp1.7.4从而自动安装好Apache和MySQL,Apache的端口可能会被IIS服务占用,可以去控制面板里关掉 2.  修改root密码,为了能在phpMyAdmin里继续操作数 ...

  7. Missing artifact com.sun:tools:jar 1.5.0 终极解决方法

    在使用m2eclipse插件时,在pom.xml中添加struts2-core.jar包后,需要依赖java运行时的tools.jar进行依赖.但是,此时eclipse无法读取tools包,出现如下错 ...

  8. Android测试AsyncTask下载图片

    package com.example.myact8_async; import org.apache.http.HttpEntity; import org.apache.http.HttpResp ...

  9. Windows 2003/2008更改远程桌面端口脚本

    保存为bat文件,点击运行按提示输入新端口自动完成,直接下载更改远程桌面端口脚本   @echo off color 0a title @@ 修改Windows XP/2003/2008远程桌面服务端 ...

  10. Linux centOS7 下安装mysql5.7.10

    1:下载二进制安装包 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 2:解压到 ...