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 ...
随机推荐
- Internet History, Technology and Security (Week 8)
Week 8 Security: Encrypting and Signing This week we start two weeks of Internet Security. It is a l ...
- PAT 1042 字符统计
https://pintia.cn/problem-sets/994805260223102976/problems/994805280817135616 请编写程序,找出一段给定文字中出现最频繁的那 ...
- jQ返回定位插件
一.jQuery 提供开发者开发插件的几种模式 1.$.extend(); //这个方法是绑定在$上面的.可以通过$直接调用 2.$.fn.方法名 //这个方法是绑定在Dom对象上面的 ...
- TP5 助手函数与TP3.2单字母函数
一.TP5 助手函数 助手函数 描述 abort 中断执行并发送HTTP状态码 action 调用控制器类的操作 cache 缓存管理 config 获取和设置配置参数 controller 实例化控 ...
- ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)
中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这 ...
- 【uoj#209】[UER #6]票数统计 组合数+乱搞
题目描述 一个长度为 $n$ 的序列,每个位置为 $0$ 或 $1$ 两种.现在给出 $m$ 个限制条件,第 $i$ 个限制条件给出 $x_i$ .$y_i$ ,要求至少满足以下两个条件之一: 序列的 ...
- P3919 【模板】可持久化数组(可持久化线段树/平衡树)
题目描述 如题,你需要维护这样的一个长度为 N 的数组,支持如下几种操作 在某个历史版本上修改某一个位置上的值 访问某个历史版本上的某一位置的值 此外,每进行一次操作(对于操作2,即为生成一个完全一 ...
- Vue设置页面的title
原文地址:http://www.cnblogs.com/JimmyBright/p/7410771.html 前端框架如Vue.React等都是单页面的应用,也就是说整个web站点其实都是一个inde ...
- DB2 Vs MySQL系列 | MySQL与DB2的数据类型对比
随着MySQL数据库的应用越来越广泛,DB2向MySQL数据库的迁移需求也越来越多.进行数据库之间迁移的时候,首先遇到的并且也是最基本最重要的就是两种数据库数据类型之间的转换. 相关阅读: 从商用到开 ...
- Web前端开发神器--WebStorm(JavaScript 开发工具) 8.0.3 中文汉化破解版
WebStorm(JavaScript 开发工具) 8.0.3 中文汉化破解版 http://www.jb51.net/softs/171905.html WebStorm 是jetbrains公司旗 ...