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 ...
随机推荐
- 第五周PSP&进度条
团队项目psp: 一.表格 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论用户界面 9:27 10:42 18 57 60 分析 ...
- 第十周(11.18-11.24)----个人项目----学习java总结2
一.获取随机数 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) ,注意这里的每一个括号最好都不要省略掉. 例: public static void main(S ...
- HTML标签参考手册
按字母顺序排列 New : HTML5 中的新标签. 标签 描述 <!--...--> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义锚. < ...
- MacOS & .DS_Store
MacOS & .DS_Store .DS_Store === Desktop Services Store https://en.wikipedia.org/wiki/.DS_Store h ...
- 使用flex布局调换两个按钮的位置
组件用的时antd的Modal组件,里面的按钮需要调换一下位置 今天发现用flex布局非常方便,代码如下: display: flex; justify-content: center; flex-f ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
- python读取写入内存方法SringIO,BytesIO
python中不仅仅可以在磁盘中写入文件,还允许直接在内存中直接写入数据:需要借助StringIO和BytesIO来实现: 1.直接操作StringIO from io import StringIO ...
- hbase 多个过滤器组合(列表)
使用FilterList要保证过滤器的顺序需要使用List<Filter> private static void mutilFilterData() throws IOException ...
- tomcat启动后过一会就自动关闭
1.打开tomcat 下的log查看关键字眼 常见问题就是端口被占用,被idea 页面启动占用了
- MT【148】凸数列
(2018浙江省赛13题) 设实数$x_1,x_2,\cdots,x_{2018}$满足$x_{n+1}^2\le x_nx_{n+2},(n=1,2,\cdots,2016)$和$\prod\lim ...