题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/

最长上升连续子序列 II

  给定一个整数矩阵(其中,有 n 行, m 列),请找出矩阵中的最长上升连续子序列。(最长上升连续子序列可从任意行或任意列开始,向上/下/左/右任意方向移动)。

样例

给定一个矩阵

[
[1 ,2 ,3 ,4 ,5],
[16,17,24,23,6],
[15,18,25,22,7],
[14,19,20,21,8],
[13,12,11,10,9]
]

返回 25

思路:记忆化搜索 + dp

  设Lics(num)表示以num开头的最长上升子连续序列的长度, 则Lics(A[x][y]) = max(Lics(A[x-1][y]), Lics(A[x][y-1]), Lics(x+1,y), Lics(x, y+1))+1;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<deque>
#include<map>
using namespace std; class Solution {
public:
/**
* @param A an integer matrix
* @return an integer
*/
int n, m, maxL;
int lics[][];
int vis[][];
int dir[][] = {{, }, {, }, {,-}, {-,}};
int dfs(vector<vector<int>>& A, int x, int y){
int maxLics = ;
vis[x][y] = ;
for(int i=; i<; ++i){
int xx = x+dir[i][];
int yy = y+dir[i][];
if(xx< || yy< || xx>=n || yy>=m) continue;
if(A[x][y] >= A[xx][yy]) continue;
if(!vis[xx][yy])
maxLics = max(maxLics, dfs(A, xx, yy));
else
maxLics = max(maxLics, lics[xx][yy]);
}
lics[x][y] = maxLics+;
if(maxL < lics[x][y]) maxL = lics[x][y];
return lics[x][y];
} int longestIncreasingContinuousSubsequenceII(vector<vector<int>>& A) {
n = A.size();
if(n == ) return ;
m = A[].size();
memset(lics, , sizeof(lics));
memset(vis, , sizeof(vis));
maxL = ;
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
if(!vis[i][j])
dfs(A, i, j);
return maxL;
}
};
/*
1 2 3 4 5
16 17 24 23 6
15 18 25 22 7
14 19 20 21 8
13 12 11 10 9
*/

lintcode 最长上升连续子序列 II(二维最长上升连续序列)的更多相关文章

  1. SGU 199 Beautiful People 二维最长递增子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...

  2. hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)

    Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...

  3. [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  4. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  5. java二维不定长数组测试

    package foxe; import javax.swing.JEditorPane;import javax.swing.JFrame; /** * @author fooxe * * @see ...

  6. 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)

    最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...

  7. codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)

    题目描述 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y ...

  8. UVALive - 7374 Racing Gems 二维非递减子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/356795 Racing Gems Time Limit: 3000MS 问题描述 You are playi ...

  9. 最长非降子序列的O(n^2)解法

    这次我们来讲解一个叫做"最长非下降子序列"的问题及他的O(n^2)解法. 首先我们来描述一下什么是"最长非下降子序列". 给你一个长度为n的数组a,在数组a中顺 ...

随机推荐

  1. WP8如何添加Newtonsoft.Json包

    WP8开发的时候如何使用Newtonsoft.Json包呢?我在网上包括官网下的DLL文件,添加引用时都给出了这样的提示: 而后在网上找到的解决办法是:使用NuGet程序包来添加. 首先点击工具--& ...

  2. JSON相关知识,转载:删除JSON中数组删除操作

    一:JSON是什么 JSONg格式:对象是一个无序的“名称/值”对的集合. 对象以括号开始,括号结束. 名称冒号分隔值. "名称/值"之间用逗号分隔 例: var people = ...

  3. dictionary 应用(绑定dgv)

    dictionary的用法://初始化添加所有车位进这队列 Utility.Effectlist.Add(); //每触发一次,用这个增加数量 Utility.Effectlist["Car ...

  4. 怎么可以让div自适应屏幕的高度?(已解决)

    主要解决问题的方法是用JS脚本. 先看布局, 一个div是首部,另一个div是主体,主体包含左侧菜单和右侧内容. 我想把主体div的高度自适应屏幕剩余区域,怎么做? 首先,获取可见区域的高度,docu ...

  5. 华为oj 购物单

    这两天断断续续敲完这个(放假的时候比较懒),一次成功有点小激动(●'◡'●)  不过貌似从第一次打开开始计时..... 这道题目很像01背包,我将附件与它们的主件绑定(就是link起来)然后套用动态规 ...

  6. C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误。

    本人今天遇到的错误,C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误. 测试了很久,才发现,是安全协议问题,把安全协议加上就可以了

  7. C# 匿名对象随笔

      最新更新请访问: http://denghejun.github.io   C#中匿名对象的一般写法是这样的: object o=new {Name="TestName"}; ...

  8. STL容器分类

  9. mongodb搭建和基本语法

    下载安装包 https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.0.0-signed.msi?_ga=1.2206 ...

  10. SQL Server2014 哈希索引原理

    SQL Server2014 哈希索引原理 翻译自:http://www.sqlservercentral.com/blogs/sql-and-sql-only/2015/09/08/hekaton- ...