POJ-2192 Zipper-順序合成串匹配

kewlgrl發表於2015-08-25
Zipper
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17283   Accepted: 6145

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<stdio.h>
#include<string.h>

#define MAX 201

int dp[MAX][MAX];
char A[MAX],B[MAX],C[MAX*2];

int main()
{
    int T;
    int i,j;
    scanf("%d",&T);
    A[0]=B[0]=C[0]='0';
    for(int cas=1; cas<=T; cas++)
    {
        scanf("%s%s%s",A+1,B+1,C+1);//這裡都從下標為1開始
        int a=strlen(A)-1,b=strlen(B)-1;
        for(i=1; i<=a; i++) //邊界處理
            if(A[i]==C[i])	dp[i][0]=1;
            else            dp[i][0]=0;
        for(i=1; i<=b; i++) //邊界處理
            if(B[i]==C[i])	dp[0][i]=1;
            else            dp[0][i]=0;
        for(i=1; i<=a; i++)
            for(j=1; j<=b; j++)
                dp[i][j]=((dp[i-1][j] && A[i]==C[i+j])||(dp[i][j-1] && B[j]==C[i+j]));
        printf("Data set %d: ",cas);
        if(dp[a][b])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
/*dp[i][j] = (A[i] == c[i+j] && dp[i-1][j] == 1) || (B[j] == c[i+j] && dp[i][j-1]==1)*/

//DFS

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 10000
using namespace std;
char word1[maxn],word2[maxn],word3[maxn];
int pos1,pos2,pos3,c;
bool flag;
void dfs(int wa,int wb,int wc)
{
    if(wa+1==0&&wb+1==0)
    {
        flag=true;
        return;
    }
    if(!flag&&word1[wa]==word3[wc]&&(wa+1))
        dfs(wa-1,wb,wc-1);
    if(!flag&&word2[wb]==word3[wc]&&(wb+1))
        dfs(wa,wb-1,wc-1);
    return ;
}
int main()
{
    int loop;
    cin>>loop;
    int cns=1;
    while(loop--)
    {
        cin>>word1>>word2>>word3;
        printf("Data set %d: ",cns++);
        flag=false;
        pos1=strlen(word1);
        pos2=strlen(word2);
        pos3=strlen(word3);
        if(word1[pos1-1]==word3[pos3-1]||word2[pos2-1]==word3[pos3-1])
            dfs(pos1-1,pos2-1,pos3-1);
        printf(flag?"yes\n":"no\n");
    }
    return 0;
}


相關文章