zoukankan      html  css  js  c++  java
  • Zipper

     

    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[210], s2[210], s3[420];
    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 = 1;
        get(n);
        while(n-- > 0){
            get(s1);
            get(s2);
            get(s3);
            ans = false;
            
            len1 = strlen(s1);
            len2 = strlen(s2);
            len3 = strlen(s3);
            if(len1 + len2 == len3 && (s1[len1-1]==s3[len3-1]||s2[len2-1]==s3[len3-1])){
                work(0, 0, 0);
            }
            if(ans){
                
                printf("Data set %d: yes
    ", kase++);
            }else{
                printf("Data set %d: no
    ", kase++);
            }
        }
        return 0;
    }
    
    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 + 1, j, k + 1);
        }
        if(j < len2 && s2[j] == s3[k]){
            work(i, j + 1, k + 1);
        }
    }

    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);
            }
        }
    }
  • 相关阅读:
    JAVA 网格布局管理器
    JAVA 流式布局管理器
    JAVA 边界布局管理器
    JAVA 图形界面 JFrame容器
    MySQL联合查询语法内联、左联、右联、全联
    ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
    ASP.NET MVC URL重写与优化(初级篇)-使用Global路由表定制URL
    MVC过滤器详解
    Dapper的基本使用
    JQuery fullcalender文档
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4709167.html
Copyright © 2011-2022 走看看