[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 ...
随机推荐
- JAVA每日一旅
1.关于final关键字 final修饰的成员变量为基本数据类型时,在赋值之后无法改变.当final修饰的成员变量为引用数据类型时,在赋值后其指向地址无法改变,但是对象内容还是可以改变的. final ...
- 20135337朱荟潼 Linux第八周学习总结——进程的切换和系统的一般执行过程
第八周 进程的切换和系统的一般执行过程 一.进程切换关键代码switch_to 1.不同类型进程有不同调度需求--两种分类 2.调度策略--规则 Linux中进程优先级是动态的,周期性调整. 3.时机 ...
- 浅谈FPGA
浅谈FPGA 前言 生活中永远都不会缺少「 为什么 」,于最近就被合胜学长了,问了一个看似简单却又极具意义的问题,为什么需要FPGA?FPGA与单片机的区别是什么?瞬间刷新了我入门三天FPGA的冲击感 ...
- ejabberd与XMPP
ejabberd支持XMPP协议. worktile用ejabberd来做了实时消息推送: https://worktile.com/tech/basic/worktile-real-time-not ...
- QEMU简单知识 以及磁盘格式转换的简单命令
From 百度百科 QEMU,是由 Fabrice Bellard开发的通用.开源机器模拟与虚拟化软件,Fabrice Bellard是另一个著名的C编译器的作者.QEMU可以在不同的机器上运行独自开 ...
- UML实践详细经典教程
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...
- js 复制到剪切板
function copyTextToClipboard(text) { var copyFrom = $('<textarea/>'); copyFrom.text(text); $(' ...
- LOJ 530 最小倍数(数论)
题意 有\(T\)组数据. 给定\(p\),求最小的正整数\(n\),使得\(n!\%p=0\). 由于\(p\)很大,输入将给出\(m\)和\(e_1,e_2...e_m\),表示\(p=\prod ...
- Square Numbers UVA - 11461(水题)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题
题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...