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

至今还在wa,已经无奈,,,,,贴着求大神找错;

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 char A[201],B[201],C[402],t1,t2,t3,flot;
 4 void dfs(int len1,int len2,int len3){
 5     if(flot)return;
 6     if(len3>=t3){
 7         flot=1;
 8         return;
 9     }
10     if(C[len3]==A[len1]){
11         dfs(len1+1,len2,len3+1);
12     }
13     if(C[len3]==B[len2]){
14         dfs(len1,len2+1,len3+1);
15     }
16 }
17 int main(){
18     int n,tot=0;
19     scanf("%d",&n);
20     while(n--){flot=0;tot++;
21     memset(A,0,sizeof(A));
22     memset(B,0,sizeof(B));
23     memset(C,0,sizeof(C));
24         scanf("%s%s%s",A,B,C);
25         t1=strlen(A);
26     t2=strlen(B);
27     t3=strlen(C);
28     if(t1+t2==t3&&(A[t1-1]==C[t3-1]||B[t2-1]==C[t3-1]))dfs(0,0,0);
29     if(flot)printf("Data set %d: yes\n",tot);
30     else printf("Data set %d: no\n",tot);
31     }
32     return 0;
33 }

大神的就能ac,感觉都差不多啊:

 1 #include<stdio.h>
 2 #include<string.h>
 3 char a[220],b[220],c[220*2];
 4 bool flag;
 5 int len1,len2,len3;
 6 void dfs(int x,int y,int len)
 7 {
 8     if(flag)
 9         return;
10     if(len>=len3)
11     {
12         flag=true;
13         return;
14     }
15     if(a[x]==c[len])
16     {
17         dfs(x+1,y,len+1);
18     }
19     if(b[y]==c[len])
20         dfs(x,y+1,len+1);
21 }
22 int main()
23 {
24     int t,cot=0;
25     scanf("%d",&t);
26     while(t--)
27     {
28         scanf("%s%s%s",a,b,c);
29         len1=strlen(a);
30         len2=strlen(b);
31         len3=strlen(c);
32         if(len1+len2!=len3)
33         {
34             printf("Data set %d: no\n",++cot);
35             continue;
36         }
37         flag=false;
38         if(a[len1-1]==c[len3-1]||b[len2-1]==c[len3-1])
39         {
40             dfs(0,0,0);
41         }
42         if(flag)
43             printf("Data set %d: yes\n",++cot);
44         else
45             printf("Data set %d: no\n",++cot);
46     }
47 }
时间: 2024-10-12 16:45:20

Zipper的相关文章

hdu1501&amp;&amp;poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1501 POJ:   http://poj.org/problem?id=2192 Zipper Description Given three strings, you are to determine whether the third string can be formed by combining the

hdu 1501 Zipper 记忆化搜索

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7188    Accepted Submission(s): 2571 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

HDOJ 题目1501 Zipper(DFS)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7223    Accepted Submission(s): 2576 Problem Description Given three strings, you are to determine whether the third string can be formed

【HDOJ】1501 Zipper

DFS.注意剪枝,0ms. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define False 0 5 #define True 1 6 #define MAXN 201 7 8 char stra[MAXN], strb[MAXN], strc[MAXN<<1]; 9 int len1, len2, len3; 10 11 int dfs(int e1, int e2, int e3) { 12 int i, fl

HDU 1501 Zipper

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 e

POJ - 2192 - Zipper (简单DP)

题目传送:Zipper 思路:设状态dp[i][j]为字符串A前i个字符和B前j个字符能否组成C的前i+j个字符,边界为dp[0][0] = 1能则为true,否则false AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <

HDU 1501 Zipper(DP,DFS)

题意  判断能否由字符串a,b中的字符不改变各自的相对顺序组合得到字符串c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示能否有a的前i个字符和b的前j个字符组合得到c的前i+j个字符  值为0或者1  那么有d[i][j]=(d[i-1][j]&&a[i]==c[i+j])||(d[i][j-1]&&b[i]==c[i+j])   a,b的下标都是从1开始的  注意0的初始化 #include<cstdio> #include<cst

zlib库剖析(1):实现概览 Zipper.cpp

// Zipper.cpp: implementation of the CZipper class. // ////////////////////////////////////////////////////////////////////// #include "Zipper.h" #include "zip.h" #include "iowin32.h" #include "common.h" #include &q