nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)
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 ;
}
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 60426 | Accepted: 27062 |
Description
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
Output
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(动态规划,演算法)的更多相关文章
- 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹
一, 最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...
- nyoj 17 单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- nyoj 题目17 单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- nyoj 单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- 【LCS,LIS】最长公共子序列、单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- NYOJ17,单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf.长度为4 输入 第 ...
- ny17 单调递增最长子序列
单调递增最长子序列时间限制:3000 ms | 内存限制:65535 KB难度:4 描述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- nyoj_17_单调递增最长子序列_201403121516
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- 单调递增最长子序列(南阳理工ACM)
描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0<n<20,表示有n个字符串要处理随后的n行,每行有一个字符串,该字符串 ...
随机推荐
- Sieve of Eratosthenes时间复杂度的感性证明
上代码. #include<cstdio> #include<cstdlib> #include<cstring> #define reg register con ...
- std::wstring
std::wstring主要用于 UTF-16编码的字符, std::string主要用于存储单字节的字符( ASCII字符集 ),但是也可以用来保存UTF-8编码的字符. UTF-8和UTF-16是 ...
- Python3+RobotFramework+pycharm环境搭建
我的环境为 python3.6.5+pycharm 2019.1.3+robotframework3.1.2 1.安装python3.x 略 之后在cmd下执行:pip install robot ...
- 设计模式C++描述----13.代理(Proxy)模式
一. 举例说明 我们有时打开一个网站时会发现有这样的现象,网站上的文字都显示出来了,但是上面的图片还没显示,要等一会才能显示. 这些未打开的图片的位置上,还是会有图片框和一些等待的信息的,这就是代理模 ...
- SpringBoot是如何加载配置文件的?
前言 本文针对版本2.2.0.RELEASE来分析SpringBoot的配置处理源码,通过查看SpringBoot的源码来弄清楚一些常见的问题比如: SpringBoot从哪里开始加载配置文件? Sp ...
- vue引入css文件报错Unrecognised input
一个vue项目中用到了swiper插件,引入swiper.css时报错 显示引入的css文件Unrecognised input ,在文件的line4,column12 . 其实是引入位置不对,样式文 ...
- SROP的一个实例
以前一直只是大概看过这种技术,没实践过,今天刚好遇到一道题,实践了一波,确实很方便 unmoxiao@cat ~/s/pd_ubuntu> r2 -A smallest 00:54:15 War ...
- 相关推导式-Python
列表.’字典等推导式 #利用zip()函数同时给多个变量赋值 a = [1,2,3,4,5] b = [4,5,6,7,8] c = [9,2,3,4,0] l = [[1,2],[3,4]] for ...
- Linux下基本操作
强行转Linux,开始以为会很不适应,其实还好,换汤不换药 本文只讲基本操作,足够让你愉快的打代码,想飞上天的自行百度,或找其他大神(友链) Update 6/20:由于写得太烂被学长爆踩了一顿 直接 ...
- 三分钟学会Redis在.NET Core中做缓存中间件
大家好,今天给大家说明如何在.NET Core中使用Redis,我们在想要辩论程序的好与坏,都想需要一个可视化工具,我经常使用的是一位国内大牛开发的免费工具,其Github地址为: https://g ...