poj2192 지퍼 두 문자열이 세 번째 문자열을 구성할 수 있는지

Zipper
Time Limit: 1000MS
 
Memory Limit: 65536K
Total Submissions: 13390
 
Accepted: 4723
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[i][j] s1 i s2 j str i+j 
//dp[i][j] dp[i-1][j] dp[i][j-1] 
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#include<string.h>
using namespace std;
char s1[205];
char s2[205];
char str[410];
int dp[205][205];
int main()
{
    int t;
    int count=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s%s",s1,s2,str);
        memset(dp,0,sizeof(dp));
        int l1=strlen(s1);
        int l2=strlen(s2);
        for(int i=1;i<=l1;i++)
        if(s1[i-1]==str[i-1])
        dp[i][0]=1;
        for(int j=1;j<=l2;j++)
        if(s2[j-1]==str[j-1])
        dp[0][j]=1;
        for(int i=1;i<=l1;i++)
        for(int j=1;j<=l2;j++)
        {
            if(s1[i-1]==str[i+j-1]&&dp[i-1][j])
            dp[i][j]=1;
            if(s2[j-1]==str[i+j-1]&&dp[i][j-1])
            dp[i][j]=1;
        }
        printf("Data set %d: ",count++);
        if(dp[l1][l2]==1)
        puts("yes");
        else
        puts("no");
    }
}
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int N=205;
int dp[N][N];
char s1[N],s2[N];
char str[2*N];
int main()
{
    int t;
    scanf("%d",&t);
    int count=1;
    while(t--)
    {
        scanf("%s%s%s",s1+1,s2+1,str+1);
        int l1=strlen(s1+1);
        int l2=strlen(s2+1);
        memset(dp,-1,sizeof(dp));
        dp[0][0]=dp[0][1]=dp[1][0]=1;
        for(int i=0;i<=l1;i++)
        for(int j=0;j<=l2;j++)
        {
            if(i!=0&&s1[i]==str[i+j]&&dp[i-1][j]==1)
            dp[i][j]=1;
            if(j!=0&&s2[j]==str[i+j]&&dp[i][j-1]==1)
            dp[i][j]=1;
        }
        printf("Data set %d: ",count++);
        if(dp[l1][l2]==1)
        puts("yes");
        else
        puts("no");
    }
}

좋은 웹페이지 즐겨찾기