题目如下:

#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的更多相关文章

  1. [Alg::DP] 袋鼠过河

    一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...

  2. [Leetcode221]最大面积 Maximal Square

    [题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  3. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  4. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

  5. hdu 1398 Square Coins(简单dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Pro ...

  6. Common Subsequence(dp)

    Common Subsequence Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 951  Solved: 374 Description A subs ...

  7. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  8. POJ2533——Longest Ordered Subsequence(简单的DP)

    Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...

  9. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

随机推荐

  1. LeetCode 504. Base 7 (C++)

    题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...

  2. 软工实践周六实践课安排(2017秋学期) | K 班

    软工实践周六实践课安排(2017秋学期) | K 班 周数 截止时间 工作内容 阶段成果展示形式 验收方式 备注 4之前 2017.10月前 组队 随笔(提供组队名单.组队队员的介绍--包括擅长的地方 ...

  3. VS2013简单的单元测试

    安装过程本人在此就不做多余的说明,如果一个程序员连一个软件都无法安装那我也醉了,其次就是希望我们不要为了完成作业而去用VS,下面我具体说一下单元测试. 第一步,文件→新建一个项目,具体操作如下图 打开 ...

  4. jquery实现点击复制到剪切板

    1.必须有先引入 jquery库 <script type="text/javascript" src="js/jquery.js"></sc ...

  5. 从零开始学Kotlin-扩展函数(10)

    从零开始学Kotlin基础篇系列文章 什么是扩展函数 扩展函数数是指在一个类上增加一种新的行为,我们甚至没有这个类代码的访问权限: Kotlin 可以对一个类的属性和方法进行扩展,且不需要继承或使用 ...

  6. Maven 学习笔记——将普通的Java项目转换成Maven项目(3)

    将一个普通的java项目转换成Maven项目并不是一个很大的任务,仅仅只需要下面的几步就能将转换成功.下面我是用一个简单的Selenium测试小demon作为例子来说的. 移调项目中所有关联的Libr ...

  7. 部分机器进入bios 的 方法

  8. [转帖]linux namespace 和cgroup lxc

    https://blog.csdn.net/xiaoliuliu2050/article/details/53443863 5.1 linux namespace 和cgroup lxc 2016年1 ...

  9. BZOJ2729 HNOI2012排队(组合数学+高精度)

    组合入门题.高精度入门题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...

  10. BZOJ 4380 [POI2015]Myjnie | DP

    链接 BZOJ 4380 题面 有n家洗车店从左往右排成一排,每家店都有一个正整数价格p[i]. 有m个人要来消费,第i个人会驶过第a[i]个开始一直到第b[i]个洗车店,且会选择这些店中最便宜的一个 ...