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. TDE与列级数据加密

    一.测试TDE此部分内容扩展SQL Server安全系列的第九篇:SQL Server安全透明数据加密的测试TDE章节.启用TDE的详细步骤请参考原文. -- Create a test databa ...

  2. [QT]抄—影像显示实验

    QtCreator新建一个Qt Application,命名为ImageView 在项目文件夹下添加gdal库,统一放在ImageView\gdal目录下. 右键单击项目,选择添加库命令,添加gdal ...

  3. 在HTML中调用iOS

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. bootstrap ace treeview树表

    html部分 <div class="widget-main padding-8" style="height:400px;overflow-y: scroll;& ...

  5. 使用 jQuery Deferred 和 Promise 创建响应式应用程序

    这篇文章,我们一起探索一下 JavaScript 中的 Deferred 和 Promise 的概念,它们是 JavaScript 工具包(如Dojo和MochiKit)中非常重要的一个功能,最近也首 ...

  6. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  7. codevs 2235 机票打折

    http://codevs.cn/problem/2235/ 2235 机票打折  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 青铜 Bronze 题解       题目描述  ...

  8. 变形--扭曲 skew()

    变形--扭曲 skew() 扭曲skew()函数能够让元素倾斜显示.它可以将一个对象以其中心位置围绕着X轴和Y轴按照一定的角度倾斜.这与rotate()函数的旋转不同,rotate()函数只是旋转,而 ...

  9. TF255466: Team Foundation Server 的配置过程无法继续。以前的更新或安装需要重

    在验证是否可以安装 SharePoint 时的提示,Error [ System Checks ] TF255466: The configuration process for Team Found ...

  10. Perl中的匹配(六)

    在Perl中,匹配的定界符如果是双斜线//,可以直接使用双斜线完成匹配操作. 如果特定条件下需要改变定界符,如改为{},[]等.需要加入m,m{},m[]等. m%^http://% 默认的模式匹配对 ...