首页 > 代码库 > HUD 1501 Zipper(记忆化 or DP)
HUD 1501 Zipper(记忆化 or DP)
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
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yes Data set 3: no
Source
Pacific Northwest 2004
记忆化和DP均可以,通过两个串合成第三个串。顺序不变。
记忆化:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> typedef long long LL; using namespace std; const int maxn=220; char s1[maxn],s2[maxn],s[maxn<<1]; int dp[maxn][maxn]; int len,len1,len2; int t; int dfs(int i,int j,int k) { if(k==len) return 1; if(dp[i][j]) return 0; dp[i][j]=1; if(s1[i]==s[k]) { if(dfs(i+1,j,k+1)) return 1; } if(s2[j]==s[k]) { if(dfs(i,j+1,k+1)) return 1; } return 0; } int main() { int l=0; scanf("%d",&t); while(t--) { scanf("%s%s%s",s1,s2,s); len1=strlen(s1); len2=strlen(s2); len=strlen(s); memset(dp,0,sizeof(dp)); if(dfs(0,0,0)) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0; }
DP:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> using namespace std; const int maxn=220; char s1[maxn],s2[maxn],s[maxn<<1]; int dp[maxn][maxn]; int main() { int t; int l=0; scanf("%d",&t); getchar(); while(t--) { scanf("%s%s%s",s1+1,s2+1,s+1); int len1=strlen(s1+1); int len2=strlen(s2+1); int len=strlen(s+1); memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=0;i<=len1;i++) { for(int j=0;j<=len2;j++) { if(i==0&&j==0) continue; if(s[i+j]==s1[i]&&i&&dp[i-1][j]) dp[i][j]=1; if(s[i+j]==s2[j]&&j&&dp[i][j-1]) dp[i][j]=1; } } if(dp[len1][len2]) printf("Data set %d: yes\n",++l); else printf("Data set %d: no\n",++l); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。