关闭

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

标签: POJHDU记忆化搜索
646人阅读 评论(0) 收藏 举报
分类:

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

题目链接:http://poj.org/problem?id=2192

题目大意:用两个字符串组成第三个字符串,要求两个字符串本身的相对位置不变,问能否组成

题目分析:一开始的想法是在串3中分先找串1判串2和先找串2判串1,结果wa了,提供一组别人给的数据:abca  acba  abacbaca
下面考虑搜索,直接搜的话倒着搜过了,正着搜超时,应该和数据有关,但是加上记忆化就怎么都不会超时了,dp[i][j]表示到第一个字符串的第i个位置和第二个字符串的第j个位置的状态有没有出现过,出现过就不用再搜了

代码:

#include <cstdio>
#include <cstring>
int const MAX = 1005;
char s1[MAX], s2[MAX], s3[MAX];
bool dp[MAX][MAX];
bool flag;

void DFS(int i1, int i2, int i3)
{
    if(flag || i3 == (int)strlen(s3))
    {
        flag = true;
        return;
    }
    if(dp[i1][i2])
        return;
    dp[i1][i2] = true;
    if(s1[i1] == s3[i3])
        DFS(i1 + 1, i2, i3 + 1);
    if(s2[i2] == s3[i3])
        DFS(i1, i2 + 1, i3 + 1);
    return;
}

int main()
{
    int T, ca = 1;
    scanf("%d", &T);
    while(T--)
    {
        memset(dp, false, sizeof(dp));
        flag = false;
        scanf("%s %s %s", s1, s2, s3);
        DFS(0, 0, 0);
        if(flag)
            printf("Data set %d: yes\n", ca ++);
        else
            printf("Data set %d: no\n", ca ++);
    }
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

HDU 1428 漫步校园(最短路+记忆化搜索)

漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub...
  • hurmishine
  • hurmishine
  • 2016-07-14 22:42
  • 1661

HDU 1501 Zipper & ACM俱乐部 2604 单词混合【记忆化搜索】

转载请注明出处:http://blog.csdn.net/a1dark 分析:这两题都是一样的、输出有点细微的差别、如果直接暴力搜索的话、会TLE、于是我们需要强力的剪枝、记忆化搜索便是一个比较好的选...
  • verticallimit
  • verticallimit
  • 2013-11-23 00:39
  • 1864

记忆化搜索(搜索+dp思想)

一:简介 (1)记忆化搜索 即 搜索+动态规划数组记录上一层计算结果,避免过多的重复计算 算法上依然是搜索的流程,但是搜索到的一些解用动态规划的那种思想和模式作一些保存;一般说来,动态规划...
  • u010700335
  • u010700335
  • 2015-03-08 20:38
  • 1525

POJ2192:Zipper(记忆化搜索)

Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18818   Accept...
  • junior19
  • junior19
  • 2017-11-24 23:22
  • 28

【记忆化搜索】HDU-1501 Zipper

给出三个字串,问第三个字串能否由前两个字串拼凑而成。顺序不能打乱。
  • u012325552
  • u012325552
  • 2014-12-01 19:18
  • 578

hdu 1501 Zipper 记忆化搜索

题意:输入三个字符串,aa,bb,cc。 cc是由aa和bb组成的。但是aa和bb在cc中的原顺序不变。问cc是否可以由aa和bb构成。 做法:dfs,要记录状态。先拿aa的当前字母去试,能匹配c...
  • u013532224
  • u013532224
  • 2015-03-09 22:08
  • 503

HDU-1501 Zipper DFS+记忆化搜索

博客园社区首页新随笔联系管理订阅 随笔- 649  文章- 10  评论- 54  HDU-1501 Zipper DFS+记忆化搜索    ...
  • Enjoying_Science
  • Enjoying_Science
  • 2014-09-15 22:54
  • 434

HDU 1501 Zipper & ACM俱乐部 2604 单词混合【记忆化搜索】

转载请注明出处:http://blog.csdn.net/a1dark 分析:这两题都是一样的、输出有点细微的差别、如果直接暴力搜索的话、会TLE、于是我们需要强力的剪枝、记忆化搜索便是一个比较好的选...
  • verticallimit
  • verticallimit
  • 2013-11-23 00:39
  • 1864

暑期dp46道(26)HDOJ 1501 Zipper dfs+记忆化搜索

题目链接:HDOJ 1501 额,这种结题方法讲了很多了,这篇就不细讲了,不过有点要做改动,就是,dfs到达第三个字符串结尾就一直退出就可以了,全局变量标记状态,或者用int dfs...
  • ADjky
  • ADjky
  • 2016-08-11 13:29
  • 162

poj2192 | HDU1501 - Zipper(最长公共子序列)

Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14117   Accepted: 4958 ...
  • yew1eb
  • yew1eb
  • 2013-05-25 14:39
  • 1533
    个人资料
    • 访问:707963次
    • 积分:14950
    • 等级:
    • 排名:第908名
    • 原创:815篇
    • 转载:7篇
    • 译文:0篇
    • 评论:122条
    博客专栏