soj1010. Zipper
1010. Zipper
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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[i][j] 表示 以s0s1s2.....s(i-1)与t0t1t2...t(j-1)构成c0c1...c(i+j-1)的可能性。
基态:
if(s[i] == c[i] ) dp[i+1][0] = 1;
if(t[j] == c[j]) dp[0][j+1] = 1;
递归态:
if(dp[i-1][j] && s[i-1] == c[i+j-1]) dp[i][j] = 1;
if(dp[i][j-1] && t[j-1] == c[i+j-1]) dp[i][j] = 1;
代码如下:
#include <iostream>
#include <string>
#include <memory.h>
using namespace std; int dp[202][202]; int main()
{
int N;
cin >> N;
int count = 0;
while(N--)
{
count++;
memset(dp,0,sizeof(dp));
string ss,tt,cc;
cin >> ss >> tt >> cc;
int i,j;
for(i = 0;i < ss.size();i++)
{
if(ss[i] == cc[i])
dp[i+1][0] = 1;
}
for(j = 0;j < tt.size();j++)
{
if(tt[j] == cc[j])
dp[0][j+1] = 1;
}
for(i = 1;i <= ss.size();i++)
{
for(j = 1;j <= tt.size();j++)
{
if(dp[i-1][j] && ss[i-1] == cc[i+j-1])
dp[i][j] = 1;
if(dp[i][j-1] && tt[j-1] == cc[i+j-1])
dp[i][j] = 1;
}
}
int len1 = ss.size();
int len2 = tt.size();
if(dp[len1][len2])
cout << "Data set " << count << ": " << "yes" << endl;
else
cout << "Data set " << count << ": " << "no" << endl;
}
return 0;
}
soj1010. Zipper的更多相关文章
- POJ 2192 :Zipper(DP)
http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- Zipper
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1501 Zipper 动态规划经典
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- hdu1501 Zipper
Zipper Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- Zipper(poj2192)dfs+剪枝
Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15277 Accepted: 5393 Descripti ...
- Haskell语言学习笔记(36)Data.List.Zipper
ListZipper 模块 $ cabal install ListZipper Installed ListZipper-1.2.0.2 Prelude> :m +Data.List.Zipp ...
- HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏
Zipper Problem Description Given three strings, you are to determine whether the third string can be ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
随机推荐
- dotnet core sdk 2.1 在centos下的安装
1. 安装微软的仓库 rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm 2. 修改仓库 ...
- 用ul li实现边框重合并附带鼠标经过效果
边框重合这个效果并不难,只是我们没有真正的动手做过而已,下面让我们来谈谈用ul li如何实现边框重合,并附带鼠标经过效果 <!DOCTYPE html> <html lang=&qu ...
- Java并发编程:线程池
一.为什么使用线程池 使用线程的时候直接就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降 ...
- MT【124】利用柯西求最值
已知 \(a\) 为常数,函数\(f(x)=\dfrac{x}{\sqrt{a-x^2}-\sqrt{1-x^2}}\) 的最小值为\(-\dfrac{2}{3}\),则 \(a\) 的取值范围___ ...
- 【刷题】BZOJ 4657 tower
Description Nick最近在玩一款很好玩的游戏,游戏规则是这样的: 有一个n*m的地图,地图上的每一个位置要么是空地,要么是炮塔,要么是一些BETA狗,Nick需要操纵炮塔攻击BETA狗们. ...
- 【ARC083E】Bichrome Tree
Description 给一棵\(n\)个节点的树,和一个长度同样为\(n\)的非负整数序列\(x_i\). 请尝试对每个节点染黑或白两种颜色,并确定一个非负整数权值. 问是否存在一种方案 ...
- BZOJ2007 [Noi2010]海拔 【平面图最小割转对偶图最短路】
题目链接 BZOJ2007 题解 这是裸题啊,,要是考试真的遇到就好了 明显是最小割,而且是有来回两个方向 那么原图所有向右的边转为对偶图向下的边 向左的边转为向上 向下转为向左 向上转为向右 然后跑 ...
- Map / HashMap 获取Key值的方法
方法1:keySet()HashMap hashmp = ne HashMap();hashmp.put("aa", "111");Set set = hash ...
- Html5 drag&drop
下面是一个拖放例子 定义如下的两个div,分别是用于拖放后放置新元素的容器和可拖动的元素: <body> <div id="holder" style=" ...
- MYSQL指定用户访问指定数据库
1.使用navicat 1)首先使用root用户新建连接 2)新建mysql用户 3)点击权限,选择添加权限,出现MySQL中已存在的数据库列表,选择你要为该新建用户开放的数据库,此处选择“maiba ...