lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接: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(二维最长上升连续序列)的更多相关文章
- SGU 199 Beautiful People 二维最长递增子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- [LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- [poj1088]滑雪(二维最长下降子序列)
解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- java二维不定长数组测试
package foxe; import javax.swing.JEditorPane;import javax.swing.JFrame; /** * @author fooxe * * @see ...
- 最长连续公共子序列(LCS)与最长递增公共子序列(LIS)
最长公共子序列(不连续) 实际问题中也有比较多的应用,比如,论文查重这种,就是很实际的一个使用方面. 这个应该是最常见的一种了,不再赘述,直接按照转移方程来进行: 按最普通的方式就是,直接构造二维矩阵 ...
- codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)
题目描述 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y ...
- UVALive - 7374 Racing Gems 二维非递减子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/356795 Racing Gems Time Limit: 3000MS 问题描述 You are playi ...
- 最长非降子序列的O(n^2)解法
这次我们来讲解一个叫做"最长非下降子序列"的问题及他的O(n^2)解法. 首先我们来描述一下什么是"最长非下降子序列". 给你一个长度为n的数组a,在数组a中顺 ...
随机推荐
- CodeIgniter 3.0问题集锦
1.由于ci 3.0的session采用文件存储,在配置好session存储的目录后,在使用时如果遇到如下session错误. 此处 fc 目录为我设置的session目录. 解决将fc目录权限设为7 ...
- 页面加载完成后,触发事件——trigger()
<button id="btn">点击我</button> <div id="test"></div> 如果页面 ...
- 【ORACLE】字符串操作 B字符串时A的一部分
select * from a where instr(a,b)>0;用于实现B字段是A字段中的某一部分的时候,要论顺序或者要相邻的字符. 如果想要不论顺序或者不相邻的字符时,定义函数可以实现: ...
- LINUX常见问题
FQA1:如何进入linux单用户模式修改root密码 进入单用户模式:1. grub进入启动画面之后,敲入“e”,把光标移动到kernel ...那一行,再敲入“e”,在kernel 一行的最后加上 ...
- 【转载】64 位 Windows 内核虚拟地址空间布局(基于 X64 CPU)
原文链接:http://shayi1983.blog.51cto.com/4681835/1734822 本文为原创翻译,原文出处为 http://www.codemachine.com/articl ...
- 分布式数据库的四分结构设计 BCDE
首先,对关系型数据库的表进行四种分类定义: Basis 根基,Content 内容, Description 说明, Extension 扩展. Basis:Baisis 表是唯一的,为了实现标准而得 ...
- 对JavaScript闭包和原型理解
最近在学js脚本的一些东西觉得里面有2个知识点比较难理解所以做了如下总结. 1.闭包 简单的理解:一个函数a ,内部有个函数b,那么这个函数b当被作为a函数的返回值得时候被外部的全局变量引用了,那么这 ...
- sql 连接数不释放 ,Druid异常:wait millis 40000, active 600, maxActive 600
Hibernate + Spring + Druid 数据库mysql 由于配置如下 <bean id="dataSource" class="com.alibab ...
- indows server 2008 多用户远程桌面连接设置(验证有效
然后,在运行框中输入 gpedit.msc 之后,点击确定或者直接按键盘上的回车键 计算机配置-->管理模板-->Windows组件---->远程桌面服务--->远程桌面会话 ...
- jQuery中事件与动画的总结
1.加载DOM 1.1.window事件 window.onload=function(){}.... 时机:其他资源都加载完毕后,再执行 $(function(){}) ……:只是 ...