若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

3140: 动态规划进阶题目之Zipper

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,332

题目描述

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

输入

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

输出

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.

样例输入

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

样例输出

Data set 1: yes
Data set 2: yes
Data set 3: no

参考代码

#include<stdio.h>
#include<string.h>
#define N 205
char str1[N];
char str2[N];
char str3[N*2];
int len1,len2,len3,ans;
int vis[N][N];
void dfs(int a,int b,int c) 
{
    if(ans)return;
    if(c==len3) 
    {
        ans=1;
        return;
    }
    if(vis[a][b])return;
    vis[a][b]=1;
    if(str1[a]==str3[c])
            dfs(a+1,b,c+1);
    if(str2[b]==str3[c])
            dfs(a,b+1,c+1);
}
int main() 
{
    int n,cas=1;
    scanf("%d",&n);
    while(n--) 
    {
        scanf("%s%s%s",str1,str2,str3);
        len1=strlen(str1);
        len2=strlen(str2);
        len3=strlen(str3);
        printf("Data set %d: ",cas);
        cas++;
        if(len1+len2!=len3) 
        {
            printf("non");
            continue;
        }
        memset(vis,0,sizeof(vis));
        ans=0;
        dfs(0,0,0);
        if(ans==1)
            printf("yesn");
        else
            printf("non");
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Skip Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist