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 //  Problem: poj2192 - Zipper
 2 //  Category: Dynamic programming
 3 //  Author: Niwatori
 4 //  Date: 2016/07/22
 5
 6 #include <iostream>
 7 #include <cstring>
 8 #include <string>
 9 using namespace std;
10
11 int main()
12 {
13     int t; cin >> t;
14     for (int i = 1; i <= t; ++i)
15     {
16         string s1, s2, s;
17         cin >> s1 >> s2 >> s;
18         int len1 = s1.length(), len2 = s2.length();
19         s1 = ‘ ‘ + s1; s2 = ‘ ‘ + s2; s = ‘*‘ + s;  // For convenience
20
21         bool dp[205][205];
22         memset(dp, 0, sizeof(dp));
23         dp[0][0] = 1;
24         for (int i = 0; i <= len1; ++i)
25             for (int j = 0; j <= len2; ++j)
26             {
27                 if (s1[i] == s[i + j])
28                     dp[i][j] = dp[i][j] || dp[i - 1][j];
29                 if (s2[j] == s[i + j])
30                     dp[i][j] = dp[i][j] || dp[i][j - 1];
31             }
32         cout << "Data set " << i << ": " << (dp[len1][len2] ? "yes" : "no") << endl;
33     }
34     return 0;
35 }

时间: 2024-10-10 05:52:07

POJ动态规划练习(计划6-8题)的相关文章

{POJ}{动态规划}

动态规划与贪心相关: {POJ}{2479}{Maximum Sum} (DP) 摘要: 题意:给定n个数,求两段连续子列的最大和.思路:先从左向右dp,求出一段连续子列的最大和,再从右向左dp,求出两段连续子列的最大和,方法还是挺经典的. {POJ}{1036}{Gansters} (DP) 摘要: 题意:有个伸缩门,门的宽度0~K,每个时间可以伸长或缩短1个单位,有N个流氓,他们在T时刻到达,如果这时门的宽度正好与他们的stoutness相等时,便可获得一定的收入,问最大的收入是多少. 思路

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

poj 1008:Maya Calendar(模拟题,玛雅日历转换)

Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, profes

POJ 1515 Street Directions --一道连通题的双连通和强连通两种解法

题意:将一个无向图中的双向边改成单向边使图强连通,问最多能改多少条边,输出改造后的图. 分析: 1.双连通做法: 双连通图转强连通图的算法:对双连通图进行dfs,在搜索的过程中就能按照搜索的方向给所有边定向,其中桥不能改造,只能保留双向边. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #includ

POJ 2405 Beavergnaw (计算几何-简单题)

Beavergnaw Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6203   Accepted: 4089 Description When chomping a tree the beaver cuts a very specific shape out of the tree trunk. What is left in the tree trunk looks like two frustums of a co

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

poj 3087 Shuffle&#39;m Up(模拟题)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6143   Accepted: 2880 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed