题目如下:

#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. PAT甲题题解-1065. A+B and C (64bit) (20)-大数溢出

    第一眼以为是大数据,想套个大数据模板,后来发现不需要.因为A.B.C的大小为[-2^63, 2^63],用long long 存储他们的值和sum. 接下来就是分类讨论:如果A > 0, B & ...

  2. 20135220谈愈敏Blog3_构造一个简单的Linux系统MenuOS

    构造一个简单的Linux系统MenuOS 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1 ...

  3. 修改servlet的模板代码

    实际开发中,这些生成的代码和注释一般我们都用不到的,每次都要手工删除这些注释和代码,很麻烦.下面以MyEclipse 2014(其实版本通用的,都可以修改)为例进行说明如何修改Servlet的模板代码 ...

  4. 1-Python3从入门到实战—基础之语法

    Python从入门到实战系列--目录 编码格式 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串 # -*- coding=utf-8 -*- 也可以 ...

  5. 一个由于springboot自动配置所产生的问题的解决

    由于我的项目里面需要使用到solr,我做了一下solr和springboot的整合,结果启动项目的时候,就报错了...报错的信息的第一行提示如下: org.springframework.beans. ...

  6. bzoj 1012: [JSOI2008]最大数maxnumber (线段树)

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 13081  Solved: 5654[Subm ...

  7. 【题解】 bzoj2115: [Wc2011] Xor (线性基+dfs)

    bzoj2115,戳我戳我 Solution: 看得题解(逃,我太菜了,想不出这种做法 那么丢个链接 Attention: 板子别写错了 又写错了这次 \(long long\)是左移63位,多了会溢 ...

  8. 安装完打开 eclipse 提示 JVM 版本较低

    在安装完 eclipse 后打开出现如下提示 解决办法: 1> 去官网下载最新版本安装 地址 2> linux 下直接下载安装 1.打开终端 2.控制台输入:su 3.控制台输入个人密码 ...

  9. 【hdu1542】线段树求矩形面积并

    分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...

  10. 解题:SCOI 2007 蜥蜴

    题面 拆点跑最大流 所有能跑出去的点连向汇点,容量为inf 原点连向所有初始有蜥蜴的点,容量为1 每根柱子拆成两个点“入口”和“出口”,入口向出口连容量为高度的边,出口向别的有高度的柱子的入口连容量为 ...