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; } } }
相关推荐
sicily 1562_LVM.cpp参考代码
中山大学 ACM sicily 1294 题目代码
sicily 1274的AC源码,通过且速度快,适合学生使用
本程序解决了Sicily平台上Queue的问题,有较好的可读性
这是C++解题常用的模板,对参加C++机试有较大帮助
本程序是中山大学sicily-1137-1145-1146-1147-1154-1157-1194的代码
西西里全部练习输入输出以及标准程序 超高精度浮点数的输出问题 关联数组
本程序是中山大学sicily 1004-1007-1010-1014-1021 参考代码
1005. 有向图边的分类 12图算法例题1000. sicily 1155. Can I Post the letteTime Limit: 1sec Mem
sicily 1817和1818的程序,各有两种方法,供参考。
本程序是中山大学sicily上1200-1221-1298-1324-1325的参考代码。
包括 sicily online judge 1149等部分题目,线性表,最小生成树,中缀转后缀并计算后缀表达式等。
sicily部分源代码,全都亲测,可通过
中大sicily online judge的刷题指南,里面介绍得很详细,不过,最近sicily好像不能外网访问了。
sicily(soj) 1022 1064 1310 1740 1876 1934 六题的源代码(数据结构综合应用题)
本cpp是sicily的1006的解题代码 这份代码以最简单的方式实现了它的功能要求 值得学习一番
112页的大礼包,sicily的部分ac代码。
Sicily的题目分类:各种题目的分类,大致方法,以及题目难度规范
sicily1007题答案,附代码解释,可以在平台上通过