`
linxiaoty
  • 浏览: 12083 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

题目分析:

这是一道很典型的动态规划的问题。题目的主要意思是,给出三个字符串,第三个字符串由前两个字符串组成,第三个字符串的字符的顺序要与第一、第二个字符串的顺序一致,不能颠倒。问题是,判断第三个字符串是否能够由前两个字符串构成。具体的样例可以参照样例输入输出。

参看代码:

#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;
		}
	}

}

 

0
9
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics