sicily1010. Zipper


1010. Zipper
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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

テーマ分析:
これは典型的な動的計画の問題である.タイトルの主な意味は、3つの文字列を与え、3番目の文字列は前の2つの文字列から構成され、3番目の文字列の文字の順序は1番目、2番目の文字列の順序と一致し、逆転できないことです.問題は、3番目の文字列が最初の2つの文字列から構成できるかどうかを判断することである.具体的なサンプルはサンプル入出力を参照できます.
参照コード:
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
using namespace std;

int main() {
	int t;
	scanf("%d", &t);
	int dp[201][201];
	for (int p = 1; p <= t; ++ p) {
		string s1, s2, s3;
		cin >> s1 >> s2 >> s3;
		int len1 = s1.size();
		int len2 = s2.size();
		int len3 = s3.size();
		int i;
		//dp      ,s1  i    s2  j   ,     s3  i+j-1 
		//  ,  ,  1,  , 0 
		//         , 
		for (i = 1; i <= len1; ++ i) {
			if (s1[i - 1] == s3[i - 1]) { 
				dp[i][0] = 1;
			}
			else {
				break;
			}
		}
		for (; i <= len1; ++ i) {
			dp[i][0] = 0;
		}
		for (i = 1; i <= len2; ++ i) {
			if (s2[i - 1] == s3[i - 1]) {
				dp[0][i] = 1;
			}
			else {
				break;
			}
		}
		for (; i <= len2; ++ i) {
			dp[0][i] = 0;
		}
		for (i = 1; i <= len1; ++ i) {
			for (int j = 1; j <= len2; ++ j) {
				if (dp[i][j - 1] && s2[j-1] == s3[i + j - 1]) {
					dp[i][j] = 1;
				}
				else if (dp[i - 1][j] && s1[i - 1] == s3[i + j - 1]) {
					dp[i][j] = 1;
				}
				else {
					dp[i][j] = 0;
				}
			}
		}
		if (dp[len1][len2]) {
			cout << "Data set " << p << ": yes" << endl;
		}
		else {
			cout << "Data set " << p << ": no" << endl;
		}
	}

}