POJ 2192

简介: Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13041   Accepted: 4560 Description Given three strings, you ...
Zipper
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13041   Accepted: 4560

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
/*
大致题意:输入三个字符串,判断前两个字符串能否组成最后一个,
使得各字母在原单词中的相对顺序不变
*/ 
#include<stdio.h>
#include<string.h>
char str1[210],str2[210],str[420];
bool vis[210][210];
bool dp(char *str1,char *str2,char *str)//原来把参数顺序传错啦
{
    int i,j;
    int len1=strlen(str1);
    int len2=strlen(str2);
    int len=strlen(str);
    if(len1+len2!=len)
        return false;
    for(i=1;i<=len1;i++)
        vis[i][0]=false;
    for(i=1;i<=len2;i++)
       vis[0][i]=false;
    vis[0][0]=true;
    for(i=0;i<=len1;i++)
        for(j=0;j<=len2;j++)
        {
            if(i>0&&str1[i-1]==str[i+j-1]&&vis[i-1][j])
                vis[i][j]=true;
            if(j>0&&str2[j-1]==str[i+j-1]&&vis[i][j-1])
                vis[i][j]=true;
        }
    return vis[len1][len2];
}
int main()
{
    int i,j,T;
    bool flag;
    scanf("%d%*c",&T);
    j=1;
    while(T--)
    {
        memset(str,0,sizeof(str));
        memset(str1,0,sizeof(str1));
        memset(str2,0,sizeof(str2));
        memset(vis,0,sizeof(vis));
        scanf("%s",str1);
       // puts(str1);
        scanf("%s",str2);
       // puts(str2);
        scanf("%s",str);
      //  puts(str);          
        flag=dp(str1,str2,str);
        /*
        for(i=0;i<15;i++)
        {  
             for(j=0;j<15;j++)
                 printf("%d ",vis[i][j]);
            putchar('\n');
        }
        */
        printf("Data set %d: %s\n",j++,flag==true?"yes":"no");
    }
    return 0;
}

 

目录
相关文章
|
7月前
|
算法
Wormholes—POJ3259
Wormholes—POJ3259
|
测试技术
poj-1218 THE DRUNK JAILER 喝醉的狱卒
自己去看看原题; 题目大意: 就是一个狱卒喝醉了,他第一趟吧所有的监狱都带开,第二趟把能把二整除的监狱关闭,第三趟操作能把三整除的监狱; 求最后能逃跑的罪犯数 输入第一个数是代表 测试数据组数 每个数据代表狱卒来回的次数 当作开关问题即可 #include using names...
1014 0
|
JavaScript
poj-1008-玛雅历
Description 上周末,M.A. Ya教授对古老的玛雅有了一个重大发现。从一个古老的节绳(玛雅人用于记事的工具)中,教授发现玛雅人使用了一个一年有365天的叫做Haab的历法。这个Haab历法拥有19个月,在开始的18个月,一个月有20天,月份的名字分别是pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu。
886 0
|
机器学习/深度学习 Windows
|
算法 存储
POJ 1014 Dividing 解答
题目详见http://poj.org/problem?id=1014 看到这道题第一反应便知道它是一道类似背包问题的题,  解法我自然而然得从背包问题的解法入手,  网上查了查,  背包问题的基本题型是01背包, 即每种...
1050 0