Zipper
|
ZipperTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem 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 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 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
|
注意一定要判最后一个字母可以匹配,要不超时。。。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n;
bool ans;
int len1, len2, len3;
char s1[], s2[], s3[];
void get(int &x){
scanf("%d", &x);
}
void get(char *s){
scanf("%s", s);
}
void work(int i, int j, int k);
int main(){ int kase = ;
get(n);
while(n-- > ){
get(s1);
get(s2);
get(s3);
ans = false; len1 = strlen(s1);
len2 = strlen(s2);
len3 = strlen(s3);
if(len1 + len2 == len3 && (s1[len1-]==s3[len3-]||s2[len2-]==s3[len3-])){
work(, , );
}
if(ans){ printf("Data set %d: yes\n", kase++);
}else{
printf("Data set %d: no\n", kase++);
}
}
return ;
} void work(int i, int j, int k) { if(ans)
return;
if(k >= len3){
ans = true;
return;
} if(i < len1 && s1[i] == s3[k]){
work(i + , j, k + );
}
if(j < len2 && s2[j] == s3[k]){
work(i, j + , k + );
}
}
java:
package com.hbc.week3; import java.util.Scanner; public class Zipper {
private static int n;
private static Scanner cin = new Scanner(System.in);
private static boolean ans;
public static void main(String[] args) {
String s1, s2, s3;
int kase = 1;
n = cin.nextInt();
while(n-- > 0){
s1 = cin.next();
s2 = cin.next();
s3 = cin.next();
ans = false;
if(s1.length() + s2.length() == s3.length() && (s1.charAt(s1.length() - 1)==s3.charAt(s3.length() - 1)
||s2.charAt(s2.length() - 1) == s3.charAt(s3.length() - 1))){
work(s1, s2, s3, 0, 0, 0);
} if(ans){ System.out.println("Data set " + (kase++) + ": yes");
}else{
System.out.println("Data set " + (kase++) + ": no");
}
}
}
private static void work(String s1, String s2, String s3, int i, int j, int k) { if(k >= s3.length()){
ans = true;
return;
} if(i < s1.length() && s1.charAt(i) == s3.charAt(k)){
work(s1, s2, s3, i + 1, j, k + 1);
}
if(j < s2.length() && s2.charAt(j) == s3.charAt(k)){
work(s1, s2, s3, i, j + 1, k + 1);
}
}
}
Zipper的更多相关文章
- POJ 2192 :Zipper(DP)
http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- 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 ...
- soj1010. Zipper
1010. Zipper Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Given three strings, yo ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
随机推荐
- Family Tree
Question A traditional constructing tree problem. Given a string to represent relationships, and pri ...
- [置顶] think in java interview番外篇-谈程序员如何修练英语
一.程序员对英语能力的重视度和能力要求应该是在各行各业中排在比较靠前的 这样说吧,英语程度的好坏直接影响着一个程序员的编程.开发.创新能力. 道理很简单: 1. 计算机和软件是用英语创造出来的 2. ...
- ISO9126 质量模型
功能性 适合性:当软件在指定条件下使用,其满足明确和隐含要求功能的能力. 准确性:软件提供给用户功能的精确度是否符合目标. 互操作性:软件与其它系统进行交互的能力. 安全性:软件保护信息和数据的安全能 ...
- Input输入字体颜色改变js(兼容IE)
从网上找的代码,自己封装了一下(前提:引用jQuery库) 方法1: HTML: <div class="box"> <div class="ipt1& ...
- c# ListView
// Attempt to run the file. System.Diagnostics.Process.Start(filename); //folderCol 可以存放一个路径的 栈(用于返回 ...
- ubuntu安装jira步骤
背景:前些日子在原来的一台云主机上已经部署了一个jira系统,使用一段时间后发现jira占用很多的系统资源,导致主机上的其他服务无法正常工作,于是老大新注册了一个云主机专门用于运行jira,可见公司对 ...
- 理解javascript 回调函数
##回调函数定义 百度百科:回调函数 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不 ...
- Interaction with the camera or the photo library
As we said before, we need a delegate to deal with the user interaction with the camera or the photo ...
- jQuery 验证实例(shopnc二次开发)
shopnc 商家用户实现添加用户与前台用户分离, jQuery 验证实例 equalTo:等于 <div id="saleRefund" show_id="1&q ...
- 引用 - PHP手册笔记
引用是什么 PHP中的引用意味着,用不同的变量名访问同一变量内容,类似于Unix的文件名和文件本身(变量名是目录条目,变量内容是文件本身,即用不同的目录条目访问同一文件),可以看做Unix文件系统中的 ...