POJ 2192 判断两个字符串是否能按顺序组合成第三个字符串 ZIPPER

本文介绍了一种使用动态规划算法解决两个字符串能否按原始顺序组合成第三个字符串的问题。通过定义状态转移方程并实现相应的C语言程序,验证了算法的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经典动态规划
现在对动态规划的子结构划分和子结构的理解,转移方程还没有明确的认识
Zipper
Time Limit:1000MS Memory Limit:65536K
Total Submissions:9409 Accepted:3218

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
/////opt[i][j]:表示str1[i],str2[j]能否组合成st3[i+j]用0,1表示;
//状态转移方程:若opt[i-1][j],opt[i][j-1]都为0,则opt[i][j]为0;
//若opt[i-1][j] ==1;且str3[i+j] == str1[i],则opt[i][j] = 1;
//若opt[i][j-1] == 1;且str3[i+j] == str2[j],则opt[i][j] = 1;
//初始化,opt[0][0] = 1;
//opt[i][0]表示st1[]的前i个字符是否都与str3[i]相符,相符则为1,否则为0;
//opt[0][i]表示st2[]的前i个字符是否都与str3[i]相符,相符则为1,否则为0;
#include<stdio.h>
#define MAX 201
int opt[MAX][MAX];

int main()
{
    int n;
    int cnt=0;
    int flag;
    //freopen("input","r",stdin);
    char str1[MAX],str2[MAX],str3[MAX*2];
    int len_str1,len_str2,len_str3;
    int i,j;
    scanf("%d",&n);
    while(n--)
    {
        cnt++;
        scanf("%s %s %s",str1,str2,str3);
        len_str1=strlen(str1);
        len_str2=strlen(str2);
        len_str3=strlen(str3);
        if(len_str1+len_str2!=len_str3)
        {
            printf("Data set %d: no/n");
            continue;
        }
        memset(opt,0,sizeof(opt));
        opt[0][0]=1;
        for(i=1;i<=len_str1;i++)
        {
            if(opt[i-1][0]==1&&str1[i-1]==str3[i-1]) opt[i][0]=1;
            else  break;
        }
        for(j=1;j<=len_str2;j++)
        {
            if(opt[0][j-1]==1&&str2[j-1]==str3[j-1]) opt[0][j]=1;
            else  break;
        }
        for(i=1;i<=len_str1;i++)
        {
            for(j=1;j<=len_str2;j++)
            {
                if((opt[i][j-1]==1&&str2[j-1]==str3[i+j-1])||(opt[i-1][j]==1&&str1[i-1]==str3[i+j-1]))
                    opt[i][j]=1;
            }
        }
        if(opt[len_str1][len_str2])  printf("Data set %d: yes/n",cnt);
        else printf("Data set %d: no/n",cnt);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值