当前位置: 首页 > news >正文

poj 2192 Zipper

题目链接:http://poj.org/problem?id=2192

Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 18658  Accepted: 6651

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都能解决:

深搜的话需要几个强有力剪枝条件

1、  第三个串最后一个字符要么是串1的最后一个字符,要么是串2的最后一个字符

2、  按照串1的顺序对串3进行搜索,若不匹配则该字符必是串2的下一个字符。

参考来源:http://www.cnblogs.com/yu-chao/archive/2012/02/26/2369052.html

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 char first[202],second[202],third[402],Left[401];
 6 int sign[402];
 7 bool flag;
 8 int check()
 9 {
10     int i,count=0;
11     int k=strlen(third);
12     for(i=0;i<k;i++)
13         if(!sign[i]) Left[count++]=third[i];
14     Left[count]='\0';
15     if(strcmp(Left,second)==0) return 1;
16     return 0;
17 }
18 int dfs(int f,int s,int t)
19 {
20     if(f>=strlen(first))
21     {
22         if(check()) flag=true;
23         return 0;
24     }
25     if(flag) return 0;
26     if(first[f]==third[s])
27     {
28         sign[s]=1;
29         if(s<strlen(third)) dfs(f+1,s+1,t);
30         sign[s]=0;
31     }
32     else
33     {
34         if(third[s]!=second[t]) return 0;//剪枝2
35     }
36     if(!flag && s<strlen(third)) dfs(f,s+1,t+1);
37     return 0;
38 }
39 int main()
40 {
41     int len1,len2,len3,Case,count=0;
42     scanf("%d",&Case);
43     while(Case--)
44     {
45         count++;
46         flag=false;
47         scanf("%s %s %s",first,second,third);
48         memset(sign,0,sizeof(sign));
49  
50         len1=strlen(first);
51         len2=strlen(second);
52         len3=strlen(third);
53  
54         if(len1+len2!=len3)
55         {
56             printf("Data set %d: no\n",count);
57             continue;
58         }
59         if(third[len3-1]!=first[len1-1] && third[len3-1]!=second[len2-1])// 剪枝1
60         {
61             printf("Data set %d: no\n",count);
62             continue;
63         }
64         dfs(0,0,0);
65         if(flag) 
66             printf("Data set %d: yes\n",count);
67         else
68             printf("Data set %d: no\n",count);
69     }
70     return 0;
71 }
View Code

动规算法:(参考来源)

最优子结构分析:如上例,如果A、B可以组成C,那么,C最后一个字母,必定是 A 或 B 的最后一个字母组成。
C去除除最后一位,就变成是否可以求出 A-1和B 或者 A与B-1 与 是否可以构成 C-1。。。
 状态转移方程:
用f[i][j] 表示 A前 i 位 和B 前j 位是否可以组成 C的前i+j位

             dp[i][j]= (dp[i-1][j]&&(a[i]==c[i+j]))||(dp[i][j-1]&&(b[j]==c[i+j]))

 1 #include<stdio.h>
 2 #include<string.h>
 3  
 4 char a[201],b[201],c[402];
 5 int la,lb,lc;
 6 int dp[201][201];
 7  
 8 int main()
 9 {
10     int ncase;
11     scanf("%d",&ncase);
12     for(int n=1; n<=ncase; n++) {
13          
14         a[0]='p';
15         b[0]='p';
16         c[0]='p';
17          
18         scanf("%s%s%s",a+1,b+1,c+1);
19          
20         la=strlen(a);
21         lb=strlen(b);
22         lc=strlen(c);
23          
24         la-=1;
25         lb-=1;
26          
27         //处理边界
28         for (int i=1; i<=la; i++)
29             if (a[i]==c[i]) dp[i][0]=1;
30          
31         for (int i=1; i<=lb; i++)
32             if (b[i]==c[i]) dp[0][i]=1;
33         //DP
34         for (int i=1; i<=la; i++)
35             for (int j=1; j<=lb; j++)
36                 dp[i][j]= (dp[i-1][j]&&(a[i]==c[i+j]))||(dp[i][j-1]&&(b[j]==c[i+j]));
37          
38         printf("Data set %d: ",n);
39         if (dp[la][lb]==1) printf("yes\n");
40         else printf("no\n");
41          
42     }
43 }

虽然代码简洁明了易理解,但是个人感觉下面的代码更准确一些。

 

来源:http://www.cnblogs.com/yu-chao/archive/2012/02/26/2369052.html

若用DP来作先定义res[i][j]=1表示串1前i个字符和串2的前j个字符能组成串3的前i+j个字符,res[i][j]=0则不能。

状态转移方程如下:

Res[i][j]= (third[i+j]==first[i] && res[i-1][j]==1) ||(third[i+j]==second[j]&&res[i][j-1]==1)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 char first[201],second[201],third[401];
 6 int res[201][201];
 7 int init(int n,int m)
 8 {
 9     int i;
10     for(i=1;i<=m;i++)
11         if(second[i]==third[i]) res[0][i]=1;
12         else break;
13     for(i=1;i<=n;i++)
14         if(first[i]==third[i]) res[i][0]=1;
15         else break;
16     return 0;
17 }
18 int dp(int n,int m)
19 {
20     int i,j;
21     for(i=1;i<=n;i++)
22         for(j=1;j<=m;j++)
23         {
24             if(third[i+j]==first[i] && res[i-1][j]) res[i][j]=1;
25             if(third[i+j]==second[j] && res[i][j-1]) res[i][j]=1;
26         }
27     if(res[n][m]) return 1;
28     return 0;
29 }
30 int main()
31 {
32     int n,len1,len2,count=0;;
33     scanf("%d",&n);
34     while(n--)
35     {
36         count++;
37         scanf("%s %s %s",first+1,second+1,third+1);
38         len1=strlen(first+1);
39         len2=strlen(second+1);
40         memset(res,0,sizeof(res));
41         init(len1,len2);
42     
43         if(dp(len1,len2))
44             printf("Data set %d: yes\n",count);
45         else
46             printf("Data set %d: no\n",count);
47     }
48     return 0;
49 }
View Code

 

相关文章:

  • centos下查看tomcat的版本号
  • 浅谈UML中常用的几种图——用例图
  • [转]项目中Struts/Spring/Hibernate的基本流程
  • mac brew 安装 nginx fpm mysql 教程
  • 微软等公司数据结构+算法面试100题--链表
  • UIPickerView的使用
  • 目标板UBI工具交叉编译
  • 一个web项目web.xml的配置中context-param配置作用
  • Puppet安装dashboard
  • 非常有用的并发控制-循环栅栏CyclicBarrier
  • PHP简易的缓存实现思路
  • Ubuntu下安装Cacti+rrdtool监控Windows
  • 整理:IE6浏览器下容器boder消失的解决办法
  • 有向无环图
  • linux服务器集群运维经验
  • 【译】理解JavaScript:new 关键字
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • crontab执行失败的多种原因
  • ERLANG 网工修炼笔记 ---- UDP
  • JavaScript的使用你知道几种?(上)
  • Mac转Windows的拯救指南
  • Vue 动态创建 component
  • 半理解系列--Promise的进化史
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 技术发展面试
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 解析 Webpack中import、require、按需加载的执行过程
  • 利用DataURL技术在网页上显示图片
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 跳前端坑前,先看看这个!!
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • ​Python 3 新特性:类型注解
  • ​卜东波研究员:高观点下的少儿计算思维
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • #FPGA(基础知识)
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (poj1.3.2)1791(构造法模拟)
  • (zt)最盛行的警世狂言(爆笑)
  • (二开)Flink 修改源码拓展 SQL 语法
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (六)软件测试分工
  • (南京观海微电子)——COF介绍
  • (三)uboot源码分析
  • (算法)Game
  • (一)u-boot-nand.bin的下载
  • ******之网络***——物理***
  • .jks文件(JAVA KeyStore)
  • .NET BackgroundWorker
  • .Net的DataSet直接与SQL2005交互
  • .NET企业级应用架构设计系列之应用服务器
  • @font-face 用字体画图标