题目链接: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. nginx的日常应用

    nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes ; 工作进程:数目. ...

  2. AngularJS中的route可以控制页面元素的改变,使多页面变成一个单页面。。。

    SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把Web应用 ...

  3. Codeforces Round #361 (Div. 2) A

    A - Mike and Cellphone Description While swimming at the beach, Mike has accidentally dropped his ce ...

  4. Win32 OpenProcess打开进程失败,返回5无权限操作

    Win32 OpenProcess打开进程失败,返回5无权限操作,相信你会碰到这样的事,在IDE中可以,单独却不可以了,其实这时就需要提权了,否则是无法打开的,OpenProcess提权至Debug即 ...

  5. 【转】博弈—SG函数

    转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofeng ...

  6. myeclipse导入项目出现jquery错误(有红叉)

    今天导入了一个项目,但是进去之后jquery出现了红叉,如图(事实上在我没调好之前两个jquery文件都有叉号) 怎么调呢?右键jquery文件,选择MyEclipse->Exclude Fro ...

  7. 测试函数用Return 返回值和用函数名返回值的区别

    '*************************************************************************'**模 块 名:工程1 - Form1'**说   ...

  8. SQL Server出现错误: 4014

    SQL Server出现错误: 4014 线下的测试机器老是报错,从errorlog里看到大量的4014错误 A fatal error occurred , output error: ). 错误: ...

  9. 眼见为实:.NET类库中的DateTimeOffset用途何在

    在 EnyimMemcachedCore(支持.NET Core的memached客户端)中实现 Microsoft.Extensions.Caching.Distributed.IDistribut ...

  10. SQL SERVER全面优化-------写出好语句是习惯

    前几篇文章已经从整体提供了诊断数据库的各个方面问题的基本思路...也许对你很有用,也许你觉得离自己太远.那么今天我们从语句的一些优化写法及一些简单优化方法做一个介绍.这对于很多开发人员来说还是很有用的 ...