poj 2192 Zipper

简介: 题目链接:http://poj.org/problem?id=2192Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18658 Accepted: 6651DescriptionGiven ...

题目链接: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

题目大意:

给出两串,从两个串取出字符重新组合,看能否组成第三个串。要求:从第一个串取出的字符在第三个串中的顺序不变,第二个串取出的字符在第三个串中的顺序也不变。

算法分析:

此题深搜和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

 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
存储 测试技术 C语言
顺序表详解(SeqList)
顺序表详解(SeqList)
|
编译器 Serverless C++
【C++】C++STL 揭秘:Strng背后的底层逻辑(一)
【C++】C++STL 揭秘:Strng背后的底层逻辑
363 1
|
存储 程序员 容器
从代码到哲学:编程中的启示与人生智慧
【8月更文挑战第31天】在编程的世界里,代码不仅仅是命令和逻辑的集合,它更像是一面镜子,反映出我们对待问题的态度和方法。本文通过探索编程中的一些基本原则和实践,揭示它们如何与我们日常生活中的决策和行为相互映射。从简单的代码片段到复杂的系统设计,每一步都蕴含着深刻的哲理,指引我们在面对生活和工作中的挑战时,如何保持清晰的思路和坚定的意志。文章将带领读者走进编程的内在世界,发现那些隐藏在代码行间的智慧和启示。
|
安全 API Python
`multiprocessing`是Python的一个标准库,用于支持生成进程,并通过管道和队列、信号量、锁和条件变量等同步原语进行进程间通信(IPC)。
`multiprocessing`是Python的一个标准库,用于支持生成进程,并通过管道和队列、信号量、锁和条件变量等同步原语进行进程间通信(IPC)。
|
JavaScript 前端开发 容器
【Vue.js】使用Element搭建首页导航&左侧菜单
【Vue.js】使用Element搭建首页导航&左侧菜单
222 0
|
前端开发 Java 应用服务中间件
《SpringBoot启动流程七》:源码分析SpringBoot如何内嵌并启动Tomcat服务器的?
《SpringBoot启动流程七》:源码分析SpringBoot如何内嵌并启动Tomcat服务器的?
888 0
《SpringBoot启动流程七》:源码分析SpringBoot如何内嵌并启动Tomcat服务器的?
|
JavaScript
vue快速入门(三) 之 v-text v-html
vue快速入门(三) 之 v-text v-html
268 0
《“域”见更美好的未来——域名筑梦互联网+》电子版地址
“域”见更美好的未来——域名筑梦互联网+
140 0
《“域”见更美好的未来——域名筑梦互联网+》电子版地址
|
传感器 IDE 开发工具
Arduino 与HC-SRO4超声波传感器的OLED握手
Arduino 与HC-SRO4超声波传感器的OLED握手
363 0

热门文章

最新文章