Zipper - HDU 1501 dp
2014-07-26 19:13
363 查看
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6474 Accepted Submission(s): 2331
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".
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
思路:dp[i][j]第一个字符串的前i个和第二个字符串的前j个能否构成第三个字符串的前i+j个。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; char s[4][310],dp[310][310]; int main() { int T,t,i,j,k,len1,len2; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%s%s%s",s[1]+1,s[2]+1,s[3]+1); len1=strlen(s[1]+1); len2=strlen(s[2]+1); if(len1+len2!=strlen(s[3]+1)) { printf("Data set %d: no\n",t); continue; } memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=0;i<=len1;i++) for(j=0;j<=len2;j++) { if(i==0 && j==0) continue; if(s[3][i+j]==s[1][i] && i>0 &&dp[i-1][j]==1) dp[i][j]=1; if(s[3][i+j]==s[2][j] && j>0 &&dp[i][j-1]==1) dp[i][j]=1; } if(dp[len1][len2]==1) printf("Data set %d: yes\n",t); else printf("Data set %d: no\n",t); } }
相关文章推荐
- HDU 1501 Zipper(DP,DFS)
- hdu 1501 Zipper dp
- hdu 1501 Zipper(DP)
- HDU--杭电--1501--Zipper--深搜、DP都好
- hdu 1501 Zipper(dfs或dp)
- HDU 1501 Zipper【DP】
- HDU 1501 Zipper(dp)
- HDU-1501 Zipper 简单DP
- HDU-1501 (POJ-2192) Zipper (DFS||DP)
- HDU--杭电--1501--Zipper--深搜、DP都好
- HDU 1501 Zipper DP -
- HDU 1501 Zipper(DP,DFS)
- HDU 1501 Zipper(DP,DFS)
- hdu 1501 Zipper(DP)
- hdu(1501) Zipper
- DP_字串匹配(HDU_1501)
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
- (step4.3.5)hdu 1501(Zipper——DFS)
- (step4.3.5)hdu 1501(Zipper——DFS)
- hdu 1501 Zipper(dfs)