题目
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: tcraeteAs 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: catrteeFinally, 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
的前 i
个字符和 字符串 b
的前 j
个字符能否组成字符串 c
的前 i+j
个字符
在以下情况 dp[i][j]
为 true
a[i] == c[i+j]
并且dp[i-1][j] == true
b[j] == c[i+j]
并且dp[i][j-1] == true
注意边界值的问题即可
最后由于 a
和 b
长度为 200
因此 c
应该长度为 400
代码
/* By:OhYee Github:OhYee Blog:http://www.oyohyee.com/ Email:oyohyee@oyohyee.com かしこいかわいい? エリーチカ! 要写出来Хорошо的代码哦~ */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <functional> using namespace std; const int maxn = 405; char a[maxn],b[maxn],c[maxn]; bool dp[maxn][maxn]; void Do() { scanf("%s%s%s",a + 1,b + 1,c + 1); memset(dp,false,sizeof(dp)); a[0] = b[0] = c[0] = ' '; int lena = strlen(a) - 1; int lenb = strlen(b) - 1; if(lena + lenb != strlen(c) - 1) { printf("no"); return; } for(int i = 0;i <= lena;i++) if(false == (dp[i][0] = (a[i] == c[i] && (i == 0 || dp[i - 1][0])))) break; for(int j = 0;j <= lenb;j++) if(false == (dp[0][j] = (b[j] == c[j] && (j == 0 || dp[0][j - 1])))) break; for(int i = 1;i <= lena;i++) for(int j = 1;j <= lenb;j++) dp[i][j] = (dp[i - 1][j] && a[i] == c[i + j]) || (dp[i][j - 1] && b[j] == c[i + j]); if(dp[lena][lenb]) printf("yes"); else printf("no"); //printf(" *%s*%s*%s* ",a,b,c); return; } int main() { int T; scanf("%d",&T); for(int i = 1;i <= T;i++) { printf("Data set %d: ",i); Do(); putchar('\n'); } return 0; }