POJ 2192 :Zipper(DP)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17585 | Accepted: 6253 |
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
题意:给出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)的更多相关文章
- POJ 1260:Pearls(DP)
http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- POJ 3858 Hurry Plotter(DP)
Description A plotter is a vector graphics printing device that connects to a computer to print grap ...
- Codeforces Gym101341K:Competitions(DP)
http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...
- HDU 5791:Two(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description Alice gets two sequences A ...
- POJ - 2385 Apple Catching (dp)
题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...
- POJ 1191 棋盘分割(DP)
题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...
- 咸鱼的ACM之路:动态规划(DP)学习记录
按挑战程序设计竞赛介绍的顺序记录一遍学习DP的过程. 1. 01背包问题 问题如下: 有N个物品,每个物品(N[i])都有一定的体积(W[i]),和一定的价值(V[i]) 现在给定一个背包,背包的容量 ...
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
随机推荐
- 第六篇 Integration Services:初级工作流管理
本篇文章是Integration Services系列的第六篇,详细内容请参考原文. 简介在前几篇文章中,我们关注使用增量加载方式加载数据.在本篇文章,我们将关注使用优先约束管理SSIS控制流中的工作 ...
- cocos2dx 3.x(多个按钮button执行同一事件的区分)
// // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/10/18. // // #ifndef ATTGamePoker_h ...
- autolayout也会锁死
This application is modifying the autolayout engine from a background thread, which can lead to engi ...
- 12C对ASM rebalance操作的优化
如果在执行"alter diskgroup"操作.或在添加.删除磁盘而引发的隐式rebalance的时,没有指定power选项,rebalance操作会使用初始化参数asm_pow ...
- Java基础之一组有用的类——使用Scanner对象(TryScanner)
控制台程序. java.util.Scanner类定义的对象使用正则表达式来扫描来自各种源的字符输入,并把输入显示为各种基本类型的一系列标记或者显示为字符串. 默认情况下,Scanner对象读取标记时 ...
- 【转】分布式理论-CAP理论
一 CAP理论简述 CAP (Consistency, Availability, Partition Tolerance,) 理论是NoSQL数据库管理系统构建的基础. 强一致性:等同于所 ...
- MVC1
- JAVA-面向对象-特性
1.封装 1.定义方式 1修饰符class类名 2类名首字母大写 2.类的成员 1属性 成员变量 可以设置默认值 第一个单词首字母小写,后面首字母大写 一般把属性设置成private 提供属性对应的g ...
- Tomcat上的项目部署到WebLogic上の注意事项
1.修改web.xml: <!-- <display-name>weboutweb</display-name> --> <!-- 注释掉 display-n ...
- 夺命雷公狗ThinkPHP项目之----企业网站2之数据库的快速设计
我们在一个项目的时候,花费最多事件的估计还是数据库的时间了,我们的数据库暂时就这样设计好了: 暂时我们的数据库就这样设计好了用下先,建好后如下所示: