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".

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

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;
}

HUD 1501 Zipper(记忆化 or DP)

时间: 2024-10-25 19:32:47

HUD 1501 Zipper(记忆化 or DP)的相关文章

POJ 2192 &amp;&amp; HDU 1501 Zipper (记忆化搜索)

Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16803   Accepted: 5994 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 tw

hdu 1501 Zipper 记忆化搜索

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7188    Accepted Submission(s): 2571 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 4960 记忆化搜索 DP

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 490    Accepted Submission(s): 180 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morni

ACM学习历程—HDU5587 Array(数学 &amp;&amp; 二分 &amp;&amp; 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后从0到末尾,每一个都加上1. 例如:a0, a1, a2 => a0, a1, a2, 1, a0+1, a1+1, a2+1 题解中是这么说的:“ 其实Ai为i二进制中1的个数.每次变化A{k+2^i}=A{k}+1,(k<2^?i??)不产生进位,二进制1的个数加1.然后数位dp统计前m个数二

HDU 1078 FatMouse and Cheese(记忆化搜索DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意:一个n*n的图,每个点都有奶酪,老鼠从(0,0)开始走,每次最多只能走k步就要停下来,停下的这个位置的奶酪数只能比上一个停留的位置大,并获取其奶酪,每次只能水平或垂直走,问最多能得到的奶酪. 解题思路:记忆化搜索,这方面还是写的太少,还要看别人才会,这个就当个例子参考吧. 1 #include<cstdio> 2 #include<cstring> 3 #include

hdu1331&amp;&amp;hdu1579记忆化搜索(DP+DFS)

这两题是一模一样的``` 题意:给了一系列递推关系,但是由于这些递推很复杂,所以递推起来要花费很长的时间,所以我要编程序在有限的时间内输出答案. w(a, b, c): 如果a,b,c中有一个值小于等于0,那么w(a, b, c)的值为1 如果a,b,c中有一个值大于20,那么w(a, b, c)的值为w(20, 20, 20) 如果a<b<c,那么w(a, b, c)=w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c) 否则w(a, b, c)=w(a-

HDU 1978 How many ways(第一道记忆化搜索+DP)

How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4610    Accepted Submission(s): 2726 Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并

HNU OJ10086 挤挤更健康 记忆化搜索DP

挤挤更健康 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 339, Accepted users: 216 Problem 10086 : No special judgement Problem description 用边长小于N的正方形方砖(注意,不要求所有的方砖大小相同,请看样例说明)不重叠地铺满N*N的正方形房间,最少要几块方砖. Input 第一行是一个整

记忆化搜索+dp(洛谷1514 引水入城2010noip提高组)

在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使居民们都尽可能饮用到清澈的湖水,现在要在某些城市建造水利设施.水利设施有两种,分别为蓄水厂和输水站.蓄水厂的功能是利用水泵将湖泊中的水抽取到所在城市的蓄水池中. 因此,只有与湖泊毗邻的第1 行的城市可以建造蓄水厂.而输水站的功能则是通过输水管线利用高度落差,将湖水从高处向低处输送.故一座城市能建造输水站