Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8072    Accepted Submission(s): 2843

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
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
 

注意一定要判最后一个字母可以匹配,要不超时。。。

代码:

#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的更多相关文章

  1. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  2. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  4. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. Zipper(poj2192)dfs+剪枝

    Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15277   Accepted: 5393 Descripti ...

  6. Haskell语言学习笔记(36)Data.List.Zipper

    ListZipper 模块 $ cabal install ListZipper Installed ListZipper-1.2.0.2 Prelude> :m +Data.List.Zipp ...

  7. 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 ...

  8. soj1010. Zipper

    1010. Zipper Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Given three strings, yo ...

  9. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

随机推荐

  1. UESTC_最少花费 2015 UESTC Training for Dynamic Programming<Problem D>

    D - 最少花费 Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  2. SQL Server 日期函数:某天是星期几?

    [鹏城万里] 发表于 www.sqlstudy.com 要得到某一天是星期几,需要用到 SQL Server 中的日期函数:datename(). 今天是星期几,例子 1: set language ...

  3. 【v2.x OGE教程 20】粒子效果

    1.介绍 粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术.而这些现象用其他传统的渲染技术难以实现的真实感的 game physics.常常使用粒子系统模拟的现象有火.爆炸.烟.水流.火花. ...

  4. 一个非常给力的播放器video-js

    video-js采用的是html5播放器. 在不支持html5的浏览器会自动切换成flash. video-js的官网http://www.videojs.com/ 看看下载的demo就知道个大概了. ...

  5. RequireJs运行原理

    在require中,根据AMD(Asynchronous Module Definition)的思想,即异步模块加载机制,其思想就是把代码分为一个一个的模块来分块加载,这样无疑可以提高代码的重用. 在 ...

  6. HEVC测试序列(百度云网盘分享)

    巧妇难为无米之炊,身为一个码农怎能碗里没有米呢?想必很多朋友都碰到下载测试序列的困惑,为了减少麻烦,现提供HEVC所有测试序列的下载,上传到百度云网盘上,方便大家下载.主要的测试序列如下: Test ...

  7. 六步实现Spring.NET 与 NHibernate 的整合

    最近刚完成一个项目,其中对数据库的架构用到的是Spring.NET 与 NHibernate相结合的产物.对于这两项技术,我自己也不是太熟悉,不过好在网上有很多关于这方面的介绍文档,在这里就不多说了. ...

  8. 解决linux top命令提示的unknown terminal type的问题

    [root@localhost bin]# top 'xterm-256color': unknown terminal type. 在网上搜索了解决方法如下: 解决办法: 1.临时办法,下次启动失效 ...

  9. mysql插入返回当前生成的主键

     1:sql中需要添加属性 keyColumn="base_price_id" keyProperty="basePriceId" useGeneratedKe ...

  10. flash的as操作XML

    //as3.0 var myXML:XML = new XML(); var XML_URL:String = "nav.config"; var myXMLURL:URLRequ ...