0811 POJ#2192 Zipper


摘要
-给定三个字符串,判断第三个字符串是否是前两个串的有序组合。
原题目(或摘要)
- Zipper

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] 为A,B第 i,j位以后的串是否能构成C的 C[i+j]后的串;最终求的就是dp[0][0];
边界dp[lena][lenb] 为合法;
dp[i][j] = dp[i+1][j]&&(A[i]==C[i+j])  ||  dp[i][j+1]&&(B[j]==C[i+j])
注意
-递推中注意数据越界的问题
日期
-20170813
附加
-
代码
-
1
#include <cstdio>
2
#include <algorithm>
3
#include <iostream>
4
#include <cstring>
5
#include <memory.h>
6
using namespace std;
7
8
#define MAX 500
9
10
char A[MAX];
11
char B[MAX];
12
char C[MAX];
13
14
bool dp[MAX][MAX];//-1- notry 1- yes 0- no
15
int lena,lenb,lenc;
16
int ptrc;
17
18
bool solve(){
19
    for(int i=lena;i>=0;i--){
20
        for(int j=lenb;j>=0;j--){
21
            if(dp[i][j]){
22
                if(i-1>=0 && A[i-1]==C[i+j-1]  ) dp[i-1][j] = true;
23
                if(j-1>=0 && B[j-1]==C[i+j-1]  ) dp[i][j-1] = true;
24
            }
25
        }
26
    }
27
}
28
29
void pre(){
30
    scanf("%s %s %s",A,B,C);
31
    lena=strlen(A);
32
    lenb=strlen(B);
33
    memset(dp,false,sizeof(dp));
34
    dp[lena][lenb]=1;
35
    solve();
36
}
37
int main(){
38
39
    int n;scanf("%d",&n);
40
    int k=n;
41
    while(k--){
42
        pre();
43
        printf("Data set %d: %s\n",n-k,dp[0][0] ? "yes":"no");
44
    }
45
    return 0;
46
}
开始的时候 通过递归的方式没有成功 
想了一早上也不知道为啥----
递归的代码
1
bool solve(int i,int j){
2
    
3
    if(dp[i][j]!=-1) return dp[i][j];
4
    if(i>lena||j>lenb) return false;
5
    
6
    ptrc=i+j;
7
    bool w1 = (solve(i+1,j) &&( A[i]==C[ptrc]) );
8
    bool w2 = (solve(i,j+1) && (B[j]==C[ptrc]) );
9
10
    dp[i][j]=w1||w2?1:0;
11
    
12
    return dp[i][j];
13
}
14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值