17-单调递增最长子序列

内存限制:64MB
时间限制:3000ms
Special Judge: No

accepted:21
submit:49

题目描述:

求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4

输入描述:

第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000

输出描述:

输出字符串的最长递增子序列的长度

样例输入:

复制

3
aaa
ababc
abklmncdefg

样例输出:

1
3
7 nyoj 17 分析(动态规划):
  ①、要求整体的最大长度,我们可以从局部的最大长度来考虑;
  ②、从左到右依次考虑,每遇到一个点就从第一位开始遍历到该点,看以这个点作为前缀是否为最大值
  ③、状态方程:dp[i] = max(dp[i], d[j] + 1); 步骤:
  ①、从左到右依次遍历每一个点;
  ②、在该点基础上再从前到后通过 dp[i] = max(dp[i], d[j] + 1) 得出该点最大的值 核心代码:
 for(int i = ; i < n; ++ i)
{
dp[i] = ; //初始化每个dp[MAXN];
for(int j = ; j < i; ++ j)
if(s[j] < s[i]) dp[i] = max(dp[i], dp[j] + ); //找出所有满足条件的s[j] ==> dp[i]最大值
ans = max(ans, dp[i]);
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <stack> using namespace std;
const int MAXN = ; int main ()
{
int t;
scanf("%d", &t);
while(t --)
{
char s[MAXN];
scanf("%s", s);
int len = strlen(s), ans = -0x3f3f3f3f, dp[MAXN];
for(int i = ; i < len; ++ i)
{
dp[i] = ;
for(int j = ; j < i; ++ j)
if (s[j] < s[i])
dp[i] = max(dp[i], dp[j] + );
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
return ;
}
※nyoj 17分析(演算法)【推荐】:
  ①、找出酱紫的序列:从左到右的排列是由ASCⅡ码递增;
  ②、且每一组相邻的点ASCⅡ之差最小,及就是最为接近 核心代码:
 cnt = ; temp[] = s[];
for(int i = ; i < n; ++ i)
{
if(temp[cnt] < s[i]) temp[++cnt] = s[i] // cnt + 1即为所求
else
{
for(int j = ; j <= cnt; ++ j)
{
if(s[i] <= temp[j])
{
temp[j] = s[i];
break;
}
}
}
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <stack> using namespace std;
const int MAXN = ; int main ()
{
int t;
scanf("%d", &t);
while(t --)
{
char s[MAXN], temp[MAXN];
scanf("%s", s); int len = strlen(s), cnt = ;
temp[] = s[];
for(int i = ; i < len; ++ i)
{
if(temp[cnt] < s[i])
{
temp[++cnt] = s[i];
continue;
} for(int j = ; j <= cnt; ++ j)
{
if(s[i] <= temp[j])
{
temp[j] = s[i];
break;
}
}
}
printf("%d\n", cnt + );
}
return ;
}
Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 60426   Accepted: 27062

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

※poj 2533 分析(演算法)【推荐】:
  ①、找出酱紫的序列:从左到右的排列是由ASCⅡ码递增;
  ②、且每一组相邻的点ASCⅡ之差最小,及就是最为接近.

核心代码:

  

 int temp[] = A[], cnt = ; // cnt + 1 即为所求
for(int i = ; i < n; ++ i)
{
if (temp[cnt] < A[i]) temp[++cnt] = A[i];
else
{
for(int j = ; i <= cnt; ++ j)
{
if(A[i] <= temp[j])
{
temp[j] = A[i]; // 保证序列ASCⅡ之和最小化
break;
}
}
}
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <map>
#include <queue> using namespace std;
const int MAXN = ;
int A[MAXN], temp[MAXN]; int main()
{
int n, cnt = ;
scanf("%d", &n);
for(int i = ; i < n; ++ i)
scanf("%d", &A[i]); temp[] = A[];
for(int i = ; i < n; ++ i)
{
if(temp[cnt] < A[i]) temp[++ cnt] = A[i];
else
{
for(int j = ; j <= cnt; ++ j)
{
if(A[i] <= temp[j])
{
temp[j] = A[i];
break;
}
}
}
}
printf("%d\n", cnt + );
return ;
}

nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)的更多相关文章

  1. 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹

    一,    最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...

  2. nyoj 17 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  3. nyoj 题目17 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  4. nyoj 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  5. 【LCS,LIS】最长公共子序列、单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  6. NYOJ17,单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf.长度为4 输入 第 ...

  7. ny17 单调递增最长子序列

    单调递增最长子序列时间限制:3000 ms  |  内存限制:65535 KB难度:4 描述    求一个字符串的最长递增子序列的长度    如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...

  8. nyoj_17_单调递增最长子序列_201403121516

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  9. 单调递增最长子序列(南阳理工ACM)

    描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串 ...

随机推荐

  1. [Luogu3868] [TJOI2009]猜数字

    题目描述 现有两组数字,每组k个,第一组中的数字分别为:a1,a2,...,ak表示,第二组中的数字分别用b1,b2,...,bk表示.其中第二组中的数字是两两互素的.求最小的非负整数n,满足对于任意 ...

  2. 3.1 C语言_实现AVL平衡二叉树

    [序] 上节我们实现了数据结构中最简单的Vector,那么来到第三章,我们需要实现一个Set set的特点是 内部有序且有唯一元素值:同时各种操作的期望操作时间复杂度在O(n·logn): 那么标准的 ...

  3. HTML5+WebGL 的加油站 3D 可视化监控

    前言 随着数字化,工业互联网,物联网的发展,我国加油站正向有人值守,无人操作,远程控制的方向发展,传统的人工巡查方式逐渐转变为以自动化控制为主的在线监控方式,即采用数据采集与监控系统 SCADA.SC ...

  4. linux文档、目录相关

    linux中常用文档的目录规则: /var 存放经常变化的文件 /home 普通用户家目录 /home/xiaoliu 小刘同学的用户家目录 /etc 存放配置文件的目录 /etc/my.cnf my ...

  5. CasperJS 前端功能测试

    CasperJS 是一个开源的导航脚本和测试组件.它提供实用的高级函数.方法和语法糖,可完成以下任务: 对浏览导航步骤的定义和排序 填写和提交表单 点击和跟踪链接 获取页面快照(或者页面中的某部分) ...

  6. 委托事件(jQuery)

    <div class="content"> <ul> <li>1</li> <li>2</li> <l ...

  7. 利用X-Forwarded-For伪造客户端IP漏洞成因及防范

    内容转载自叉叉哥https://blog.csdn.net/xiao__gui/article/details/83054462 问题背景 在Web应用开发中,经常会需要获取客户端IP地址.一个典型的 ...

  8. 数据结构(三十二)图的遍历(DFS、BFS)

    图的遍历和树的遍历类似.图的遍历是指从图中的某个顶点出发,对图中的所有顶点访问且仅访问一次的过程.通常有两种遍历次序方案:深度优先遍历和广度优先遍历. 一.深度优先遍历 深度优先遍历(Depth_Fi ...

  9. Mycat分布式数据库架构解决方案--Linux安装运行Mycat

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! Myc ...

  10. 货物移动BAPI:BAPI_GOODSMVT_CREATE报错提示“不能执行功能模块 MB_CREATE_GOODS_MOVEMENT”的原因

    在开发过程中,我们调用BAPI:BAPI_GOODSMVT_CREATE进行货物移动生成物料凭证时,出现了报错提示:“不能执行功能模块 MB_CREATE_GOODS_MOVEMENT”,如下图所示: ...