DP练习 Poj2192 Zipper

题目描述:http://poj.org/problem?id=2192

解题思路;

1.book[i][j]表示str2的前i个元素和str1的前j个元素是否能组成Str_Link的前(i+j)个元素所组成的数组;

如果是,则book[i][j]=1,否则,book[i][j]=0;

2.book[0][j]表示str1的前j个元素是否能组成Str_Link的前j个元素所组成的数组;

book[i][0]表示str2的前i个元素是否能组成Str_Link的前i个元素所组成的数组;

3.状态转移方程:

  if(!book[i-1][j]&&!book[i][j-1]) continue;

                  if(book[i-1][j]&&str2[i]==Str_Link[i+j]) book[i][j]=1;

                  if(book[i][j-1]&&str1[j]==Str_Link[i+j]) book[i][j]=1;

例子分析:

str1:47935       str2:25645439    Str_Link:2564745934359

book的初始状态:

松弛后的book:

                            

 AC代码:

           

 1 #include<stdio.h>
 2 #include<string.h>
 3
 4 char str1[202],str2[202];
 5 char Str_Link[404];
 6
 7 int book[202][202];
 8 int stL1,stL2;
 9
10 void get_book()//初始化book
11 {
12
13     int i;
14
15     stL1=strlen(str1+1);
16     stL2=strlen(str2+1);
17
18     for(i=1;i<=stL1;i++)
19     {
20
21         if(book[0][i-1]&&str1[i]==Str_Link[i])  book[0][i]=1;
22         else break;
23
24     }
25
26     for(i=1;i<=stL2;i++)
27     {
28
29         if(book[i-1][0]&&str2[i]==Str_Link[i])  book[i][0]=1;
30         else break;
31
32     }
33
34
35 }
36
37 int main()
38 {
39
40     int T,count=0;
41     int i,j;
42
43     scanf("%d",&T);
44
45     while(T--){
46
47         count++;
48         memset(book,0,sizeof(book));
49
50         book[0][0]=1;
51
52         scanf("%s%s%s",str1+1,str2+1,Str_Link+1);
53
54         get_book();
55
56         for(i=1;i<=stL2;i++)
57         {
58
59             for(j=1;j<=stL1;j++)
60             {
61
62                 if(!book[i-1][j]&&!book[i][j-1]) continue;
63
64                 if(book[i-1][j]&&str2[i]==Str_Link[i+j])  book[i][j]=1;
65
66                 if(book[i][j-1]&&str1[j]==Str_Link[i+j]) book[i][j]=1;
67
68             }
69
70         }
71
72         if(book[stL2][stL1]) printf("Data set %d: yes\n",count);
73         else printf("Data set %d: no\n",count);
74
75     }
76
77     return 0;
78
79 }
80
81 /*
82
83   1
84
85   47935
86
87   25645439
88
89   2564745934359
90
91
92   */

测试数据:

时间: 2024-08-24 04:08:11

DP练习 Poj2192 Zipper的相关文章

hdu1501&amp;&amp;poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1501 POJ:   http://poj.org/problem?id=2192 Zipper Description Given three strings, you are to determine whether the third string can be formed by combining the

hdu1501&amp;amp;&amp;amp;poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1501 POJ:   http://poj.org/problem?id=2192 Zipper Description Given three strings, you are to determine whether the third string can be formed by combining the

POJ动态规划练习(计划6-8题)

Problems POJ2192 - Zipper Solutions POJ2192 - Zipper 题目大意:给定字符串s1, s2和s,问s1和s2是否满足:均为s的子序列(不必连续)且恰能构成s? 用布尔变量 dp[i][j] 表示 s1 中前 i 个字符和 s2 中前 j 个字符能否构成 s 的前 i + j 个字符构成的子串 s',显然 s' 的最后一个字符只能是 s1[i] 和 s2[j] 中的一个,由此可以写出状态转移方程.边界条件为 dp[0][0] = 1. 1 // Pr

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

POJ - 2192 - Zipper (简单DP)

题目传送:Zipper 思路:设状态dp[i][j]为字符串A前i个字符和B前j个字符能否组成C的前i+j个字符,边界为dp[0][0] = 1能则为true,否则false AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <

HDU 1501 Zipper(DP,DFS)

题意  判断能否由字符串a,b中的字符不改变各自的相对顺序组合得到字符串c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示能否有a的前i个字符和b的前j个字符组合得到c的前i+j个字符  值为0或者1  那么有d[i][j]=(d[i-1][j]&&a[i]==c[i+j])||(d[i][j-1]&&b[i]==c[i+j])   a,b的下标都是从1开始的  注意0的初始化 #include<cstdio> #include<cst

HUD 1501 Zipper(记忆化 or DP)

Problem 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 e

poj 2192 Zipper(区间dp)

题目链接:http://poj.org/problem?id=2192 思路分析:该问题可以看做dp问题,同时也可以使用dfs搜索求解,这里使用dp解法: 设字符串StrA[0, 1, …, n]和StrB[0,1, .., m]构成字符串Str[0, 1, … , m + n + 1]; 1)状态定义:dp[i, j]表示字符串StrA[0, 1, …, i-1]和字符串StrB[0, 1, .., j-1]构成字符串Str[0, 1, …, i+j-1]: 2)状态转移:如果dp[i-1][

hdu 1501 Zipper(DP)

题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,str1的前i个字符和str2的前j个字符一定构成str3的前i+j个字符.(因为拼接必须保证字符的顺序不变) 所以,,,这算是个变形的最长公共子序列? DP方程:dp[i][j]:str3的前i+j个字符能否由str1的前i个字符和str2的前j个字符拼接而成.布尔型. 看代码,, 代码: char