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. python学习-模块与包(九)

    9.4查看模块内容 dir(): 返回模块或类所包含的全部程序单元(包括变量.函数.类和方法等) __all__:模块本身提供的变量,不会展示以下划线开头的程序单元.另使用from xx import ...

  2. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  3. 百万年薪python之路 -- HTML基础

    一. Web标准 web标准: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web标准规范的分类:结构标准.表现标准.行为标准. 结构:html.表示:c ...

  4. 百万年薪python之路 -- 数据库初始

    一. 数据库初始 1. 为什么要有数据库? ​ 先来一个场景: ​ 假设现在你已经是某大型互联网公司的高级程序员,让你写一个火车票购票系统,来hold住十一期间全国的购票需求,你怎么写? 由于在同一时 ...

  5. PHP的陷阱

    PHP的陷阱 写代码的时候有个疑惑,那就是数组下标不存在的时候就会挂掉Undefined Index XXXX,这是对的,但是有时候他就不报错,这又是矛盾的. 请看下面的例子: $json_raw = ...

  6. 【原创】怎样才能写出优雅的 Java 代码?这篇文章告诉你答案!

    本文已经收录自 JavaGuide (59k+ Star):[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 本文比较简短,基本就是推荐一些对于写好代码非常有用的文章或者 ...

  7. Python+requests+unittest+excel实现接口自动化测试框架(摘录)

    一.框架结构:  工程目录 二.Case文件设计 三.基础包 base 3.1 封装get/post请求(runmethon.py) 1 import requests 2 import json 3 ...

  8. EXCEL批量导入到Sqlserver数据库并进行两表间数据的批量修改

    Excel 大量数据导入到sqlserver生成临时表并将临时表某字段的数据批量更新的原表中的某个字段 1:首先要对EXCEL进行处理 列名改成英文,不要有多余的列和行(通过ctrl+shift 左或 ...

  9. 暑期集训20190730 取模(mod)

    [题目描述] 给定一个长度为n的非负整数序列a,你需要支持以下操作: 1:给定l,r,输出a[l]+a[l+1]+…+a[r]. 2:给定l,r,x,将a[l],a[l+1],…,a[r]对x取模. ...

  10. CF464D World of Darkraft - 2

    Roma 在游戏"World of Darkraft"(理论上应该是 World of darkcraft,MineCraft 的一个版本)找到一个新角色. \(\mathrm{R ...