Zipper
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6556 Accepted Submission(s): 2361
Problem 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".
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.
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.
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
3cat tree tcraetecat tree catrteecat tree cttaree
Sample Output
Data set 1: yesData set 2: yesData set 3: no
Source
题目的意思是,第一行输入T组数据,然后没组数据输入三串字符串,第三个字符串的长度肯定都等于前两个字符串长度的总和。然后问你,把字符串1和字符串2中的相互穿插(原本字符串的顺序不变),看能不能形成字符串三,能的话,输出yes,不能的话输出no。
状态:DP[i][j],i表示字符串1的前i个字符匹配,j表示字符串2的前j个字符匹配,DP[i][j]=1则是表示字符串1匹配到第i个位置,j同理;
记忆化搜索:类似于深搜的方法加上动态规划,把每一次搜索的结构,保存在一个数组中,如果该结果又被搜索过了的,则跳过。
算法复杂度:O(N)?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define N 405 5 int Len3,DP[N][N];/*DP[i][j],i表示字符串1的前i个字符匹配,j表示字符串2的前j个字符匹配,*/ 6 char str1[N],str2[N],STR[N*2];/*DP[i][j]=1则是表示字符串1匹配到第i个位置,j同理*/ 7 int DFS(int i,int j,int k) /*i表示字符串匹配到第i位置,j同理,k表示字符串3已经匹配的长度*/ 8 { 9 if(k==Len3)return 1; /*当字符串3的匹配长度等于总长度,说明匹配成功*/ 10 if(DP[i][j])return 0; /*判断当前的记录的匹配是否存在,已经匹配过的话,直接结束,反之,记录位置*/ 11 DP[i][j]=1; 12 if(str1[i]==STR[k]) /*判断字符串1当前位置是否与字符串3匹配,若匹配的话,进行下一步匹配*/ 13 { 14 if(DFS(i+1,j,k+1)) /*如果该步匹配能够完成,则返回1,直到最初那一层,反正,进行下一步操作*/ 15 return 1; 16 } 17 if(str2[j]==STR[k]) /*判断字符串2当前位置是否与字符串3匹配,若匹配的话,进行下一步匹配*/ 18 { 19 if(DFS(i,j+1,k+1)) /*如果该步匹配能够完成,则返回1,直到最初那一层,反正,进行下一步操作*/ 20 return 1; 21 } 22 return 0; 23 } 24 int main() 25 { 26 int T,t=1; 27 scanf("%d",&T); /*输入T组测试样例*/ 28 while(T--) 29 { 30 scanf("%s %s %s",str1,str2,STR);/*每一组测试样例,输入三串字符串*/ 31 Len3=strlen(STR); /*计算标准匹配字符串的长度*/ 32 printf("Data set %d: ",t++); /*输出第几组测试样例*/ 33 memset(DP,0,sizeof(DP)); /*每次需要初始化DP[][]*/ 34 if(DFS(0,0,0)) /*如果DPS返回值为1说明能够匹配完成,反之无法匹配*/ 35 printf("yes\n"); 36 else 37 printf("no\n"); 38 } 39 return 0; 40 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************