[Alg::DP] Square Subsequence
题目如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// use this struct to store square subsequence, 4 positions and 1 length
struct SqSb {
// take square subsequence as two subsquence s0 and s1
int s00; // the position of s0's first char
int s01; // the position of s0's last char
int s10;
int s11;
int len;
SqSb() {
s00 = s01 = s10 = s11 = 0;
len = 0;
}
SqSb(int t00, int t01, int t10, int t11, int length) {
s00 = t00;
s01 = t01;
s10 = t10;
s11 = t11;
len = length;
}
};
int maxSqSubLen(const string & str) {
int strLen = str.size();
// corner cases
if (strLen < 1) return 0;
if (strLen == 2) {
if (str[0] == str[1]) return 2;
else return 0;
}
// corner cases end
// dp[i] stores the square subsequence of length (i + 1) * 2
vector<vector<SqSb> > dp;
// dp1 == dp[0] is the initial data
vector<SqSb> dp1;
for (int i = 0; i < strLen - 1; ++i) {
char ich = str[i];
for (int j = i + 1; j < strLen; ++j) {
if (ich == str[j]) {
SqSb s(i, i, j, j, 2);
dp1.push_back(s);
}
}
}
// there is no duplicate char in this string return
if (dp1.empty()) return 0;
dp.push_back(dp1);
for (int l = 2; l <= strLen/2; ++l) {
vector<SqSb> dpl;
for (int i = 0; i < dp[l - 2].size(); ++i) {
SqSb si = dp[l - 2][i];
for (int j = 0; j < dp1.size(); ++j) {
SqSb sj = dp1[j];
if (sj.s00 > si.s01 && sj.s00 < si.s10
&& sj.s10 > si.s11) {
SqSb s(si.s00, sj.s00, si.s10, sj.s10, l * 2);
dpl.push_back(s);
}
}
}
if (dpl.empty()) return (l - 1) * 2;
dp.push_back(dpl);
}
return strLen/2 * 2;
}
int main(int argc, char **argv) {
cout << maxSqSubLen(string(argv[1])) << endl;
return 0;
}
参考的是 stackoverflow 的一个提问:https://stackoverflow.com/questions/10000226/square-subsequence
题目不难,知道DP的整体流程,但是分析问题的能力差了一点。
[Alg::DP] Square Subsequence的更多相关文章
- [Alg::DP] 袋鼠过河
一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...
- [Leetcode221]最大面积 Maximal Square
[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- BestCoder Round #87 1002 Square Distance[DP 打印方案]
Square Distance Accepts: 73 Submissions: 598 Time Limit: 4000/2000 MS (Java/Others) Memory Limit ...
- hdu 1398 Square Coins(简单dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Pro ...
- Common Subsequence(dp)
Common Subsequence Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 951 Solved: 374 Description A subs ...
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- POJ2533——Longest Ordered Subsequence(简单的DP)
Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...
- HDU4632:Palindrome subsequence(区间DP)
Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...
随机推荐
- Linux 第六周实验
姬梦馨 原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.进程控制块PCB——task_st ...
- 四则运算app总结
程序有难度和单项练习,但设计了每次只出5到题,如果做错的话会把错题加入到数据库中,然后通多错题巩固选项可以对错题进行训练. 代码: 普通选项代码: package com.example.szys; ...
- mysql左外连接
左外连接的概念性不说了,这次就说一说两个表之间的查询步骤是怎么样的? 例如 SELECT ut.id,ut.name,ut.age, ut.sex,ut.status,st.score,st.subj ...
- Beta阶段 冲刺博客合集
一.Beta阶段敏捷冲刺前准备 二.Beta阶段敏捷冲刺① 三.Beta阶段敏捷冲刺② 四.Beta阶段敏捷冲刺③ 五.Beta阶段敏捷冲刺④ 六.Beta阶段敏捷冲刺⑤ 七.用户使用调查报告 八.码 ...
- Docker(二十一)-Docker Swarm集群部署
介绍 Swarm 在 Docker 1.12 版本之前属于一个独立的项目,在 Docker 1.12 版本发布之后,该项目合并到了 Docker 中,成为 Docker 的一个子命令.目前,Swarm ...
- 51nod 1476 括号序列的最小代价(贪心+优先队列)
题意 我们这有一种仅由"(",")"和"?"组成的括号序列,你必须将"?"替换成括号,从而得到一个合法的括号序列. 对于 ...
- LOJ#6118 鬼牌
\(\rm upd\):是我假了...这题没有爆精...大家要记得这道题是相对误差\(10^{-6}\)...感谢@foreverlasting的指正. 题是好题,可是标算爆精是怎么回事...要写的和 ...
- BZOJ2435 NOI2011道路修建
要多简单有多简单.然而不知道为啥在luogu上过不掉. #include<iostream> #include<cstdio> #include<cmath> #i ...
- BZOJ2749 HAOI2012外星人(数论)
不妨把求φ抽象成把将每个位置上的一个小球左移一格并分裂的过程,那么即求所有球都被移到1号格子的步数. 显然要达到1必须先到达2.可以发现每次分裂一定会分裂出2号位的球,因为2以外的质数一定是奇数.以及 ...
- Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)
题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是 在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...