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. shark恒破解笔记3-EAX决定胜负

    PEID查壳 od载入 输入假的注册码 查找出错字符串 往上查找是否有关键跳转和关键call 可以看到此处有个je跳转 实现了跳转,并且跳过了我们注册成功的地址 网上查找这个跳转的关键call,这个c ...

  2. PHP 调试脚本

    如果想要通过php.exe直接运行和调试脚本,可以在PHPStorm配置如下: 1.PHP安装XDebug的扩展. 2.在PHPStorm中,配置XDebug: 1) 打开菜单 "文件&qu ...

  3. 解决seajs ie8 对象不支持charAt 属性。

    在使用 seajs做项目,今天偶然发现在ie9以下的ie版本会 报出 对象不支持charAt 属性.刚开始还以为是自己写的js部分出了问题,经过几个小时的奋战.最终找到了其根源.在sea-debug. ...

  4. luogu P3936 Coloring

    [返回模拟退火略解] 题目描述 在一个 n×mn\times mn×m 的矩阵中,每个点都染了一种颜色(只能是 [1,c][1,c][1,c] 中的一种),求一种方案,使得相邻异色点对数最小. Sol ...

  5. php服务器有哪些

    服务器按照功能可以分为:文件服务器.数据库服务器.web服务器.邮件服务器.代理服务器..... 而上述所有的服务器,均可以用php做开发,比如说做web服务器,常用的构架是php+Mysql+Apa ...

  6. c语言1博客作业02

    c语言1博客作业02 这个作业属于哪个课程 C语言程序设计 这个作业的要求在哪 [作业要求](https://edu.cnblogs.com/campus/zswxy/SE2019-2/homewor ...

  7. Node.js事件的正确使用方法

    前言 事件驱动的编程变得流行之前,在程序内部进行通信的标准方法非常简单:如果一个组件想要向另外一个发送消息,只是显式地调用了那个组件上的方法.但是在 react 中用的却是事件驱动而不是调用. 事件的 ...

  8. SpringBoot学习(三)探究Springboot自动装配

    目录 什么是自动装配 何时自动装配 原理分析 注:以下展示的代码springboot的版本为2.0.3版.因源码过长,大家选择展开代码 ㄟ( ▔, ▔ )ㄏ 什么是自动装配 自动装配还是利用了Spri ...

  9. Codeblocks 等软件 修改源代码后 不能立即执行的解决办法||exe文件删除慢

    不懈地奋斗了两天,终于找到原因了. 记录如下 症状: Codeblocks .Visual Studio 都出现此问题:修改源代码 无法立即执行 ,就是:cannot open output file ...

  10. 关于javascript闭包的最通俗易懂的理解

    这两天在研究闭包,网上一通找,有牛人写的帖子,有普通人写的帖子,但是大多没戳中本小白所纠结的点,而且大多插入了立即执行函数,其实根本不需要的,反而让人产生了误解.这里我用我的方式讲解一下闭包. 1.目 ...