摘要
-给定三个字符串,判断第三个字符串是否是前两个串的有序组合。
原题目(或摘要)
-
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".
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".
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.
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.
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.
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.
3 cat tree tcraete cat tree catrtee cat tree cttaree
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
附加
-
代码
-
开始的时候
通过递归的方式没有成功
想了一早上也不知道为啥----
递归的代码