最长上升子序列:2016 Pacific Northwest Region Programming Contest—Division 2 Problem M
Description
A string of lowercase letters is calledalphabeticalif deleting zero or more of its letters can result inthealphabet string"abcdefghijklmnopqrstuvwxyz".
Given a strings, determine the minimum number of letters to insert anywhere in the string tomake it alphabetical.
Input
The input consists of one or more test cases,Each test case consists of one line containing of a strings(1<= |s| <=50).
It is guaranteed thatsconsists of lowercase ASCII letters `a' to `z' only.
Output
Print, on a single line, a single integer indicating the minimum number of letters that must beinserted in order to make the stringsalphabetical
Sample
//Sample Input
xyzabcdefghijklmnopqrstuvw
aiemckgobjfndlhp //Sample Output
题意:
给定一个字符串,通过删除一些元素,再添加一些元素,使之成为字母表,即"abcdefghijklmnopqrstuvwxyz",问最少需要添加多少元素
思路:
求最长上升子序列,用26减去最长的上升子序列的值即结果。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#include<stdio.h>
int main()
{
char c[];
int n,i,j,num,maxx[];
while(~scanf("%s",c))
{
num=;
for(i=; i<; ++i)
{
maxx[i]=;//数组存以第i个数为终点的最长上升序列 并且初始化为1
}
for(i=; c[i]!=; ++i)
for(j=; j<i; ++j)
{
if(c[j]<c[i]&&maxx[j]+>maxx[i])////在前i-1个序列中,寻找以终点小于c[i]的最长的子序列,即最优子状态
maxx[i]=maxx[j]+;
if(num<maxx[i])
num=maxx[i];//num用来记录最优序列长度
}
printf("%d\n",-num);
}
return ;
}
最长上升子序列:2016 Pacific Northwest Region Programming Contest—Division 2 Problem M的更多相关文章
- 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...
- 2021.12.07 [TJOI2013]最长上升子序列(Treap+DP)
2021.12.07 [TJOI2013]最长上升子序列(Treap+DP) https://www.luogu.com.cn/problem/P4309 题意: 给定一个序列,初始为空.现在我们将1 ...
- 2016 China Collegiate Programming Contest Final
2016 China Collegiate Programming Contest Final Table of Contents 2016 China Collegiate Programming ...
- 2016级算法第四次上机-F.AlvinZH的最“长”公共子序列
940 AlvinZH的最"长"公共子序列 思路 DP,难题. \(dp[i][j]\) :记录A的前i个字符与B的前j个字符变成相同需要的最小操作数. 初始化:dp[i][0] ...
- 简单Dp----最长公共子序列,DAG最长路,简单区间DP等
/* uva 111 * 题意: * 顺序有变化的最长公共子序列: * 模板: */ #include<iostream> #include<cstdio> #include& ...
- UVA10599:Robots(II)(最长上升子序列)
Your company provides robots that can be used to pick up litter from fields after sporting events an ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
随机推荐
- bzoj 2762: [JLOI2011]不等式组——树状数组
旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了,所以旺喵想挑战旺汪.旺喵给出一组一元不等式,并给出一个数值 .旺汪需要回答 ...
- perl 在win下输出中文乱码问题
use utf8; my $name = '你好'; binmode(STDOUT, ":encoding(gbk)"); print $name,"\n"; ...
- 【Python学习笔记】异常处理try-except
Python异常处理 我们一般使用try-except语句来进行异常处理. 使用except Exception as err可以统一捕捉所有异常,而也可以分开处理单个异常. # 分开捕捉单个异常 t ...
- SD 模拟sip 读写子程序
void simulate_spi_write_byte(u8 data){ u8 kk; SPI3_CS(0); SPI3_SCK(0); delay_us(1); //???spi???1/2us ...
- 2017多校第8场 HDU 6133 Army Formations 线段树合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6133 题意:给你一棵n个节点的二叉树,每个节点有一个提交任务的时间,每个节点总的提交任务的罚时为:提交 ...
- C后端设计开发 - 第5章-内功-数据结构下卷
正文 第5章-内功-数据结构下卷 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了.
- (转)opencv 代替caffe.io.load_image
self.net.blobs[, , self.image_resize, self.image_resize) #image = caffe.io.load_image(image_file) im ...
- Linux操作系统中内存buffer和cache的区别--从free命令说起(转)
原文链接:http://os.51cto.com/art/200709/56603.htm 我们一开始,先从Free命令说起. Free free 命令相对于top 提供了更简洁的查看系统内存使用情况 ...
- css文件放在头部的原因
我在博问上发的一个这个问题 然后有人这样回复我的 我感觉很有道理的样子 所以我放上来了 这样会先加载css的样式,在渲染dom的时候已经知道了自己的样式了,所以一次渲染成功 如果css放在底部,那么需 ...
- .net/c#常用框架/中间件简介(不定时更新)
任务调度 Quartz.NET:Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允 许开发人员根据时间间隔 ...