PKU2192 Zipper(DP+记忆化)

/*
 

Zipper
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 6751        Accepted: 2244

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

Source
Pacific Northwest 2004
*/

 

整个就是从左向右,当出现某个位置S3即不和当前的S1[I]相等又不和当前的S2[J]相等的时候,我们就可以判断这个时候不对了,f[i][j] = 0,接着就往回推到之前的某个分支可以选择其他的地方再开始算(指的是这个意思:比如某个时候(分支)是选择i + 1,这下i + 1推到后来推出0了,那么一路f[i][j] = 0推回到这个分支这次选择j + 1,然后往下推),注意每次递归的时候我们先判断的是当前f[i][j]是不是已经算过了,就是这段代码:

    if(f[i][j]!= -1)
    {
    return f[i][j];
    }

因为当-1的时候是没算过的时候,当为其他值(不管1还是0)就是算过,算过就马上返回不要重复再算。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值