Zipper
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 4
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, buteach 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
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yes Data set 3: no这个题目还是要用搜索的,本来我以为用字符串的方法也可以轻松水过,但是我真的错了,这还是要用dfs,因为它出现了相同的情况,很容易选择错啊,这是我做的抽象的DFS第一道题。其实BFS也是暴力求出来的,看代码吧63.#include<iostream> 64.#include<cstdio> 65.#include<cstring> 66.using namespace std; 67.char a[1005]; 68.char b[1005]; 69.char c[1004]; 70.int maze[1000][1000]; 71.int lc,la,lb; 72.int flag; 73.void dfs(int x,int y,int z) 74.{ 75. if(flag) 76. return; 77. if(z==lc) 78. { 79. flag=1; 80. return; 81. } 82. if(maze[x][y]) 83. return; 84. maze[x][y]=1; 85. if(a[x]==c[z]) 86. dfs(x+1,y,z+1); 87. if(b[y]==c[z]) 88. dfs(x,y+1,z+1); 89. 90. 91. 92.} 93.int main() 94.{ 95. int i,j,k,t; 96. scanf("%d",&t); 97. k=1; 98. while(t--) 99. { 100. scanf("%s%s%s",a,b,c); 101. flag=0; 102. lc=strlen(c); 103. la=strlen(a); 104. lb=strlen(b); 105. if(la+lb!=lc) 106. { 107. printf("no\n"); 108. continue; 109. } 110. memset(maze,0,sizeof(maze)); 111. dfs(0,0,0); 112. if(flag) 113. { 114. printf("Data set %d: yes\n",k); 115. } 116. else 117. { 118. printf("Data set %d: no\n",k); 119. } 120. k++; 121. 122. } 123. return 0; 124.}