最长上升子序列: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 ...
随机推荐
- [uva11137]立方数之和·简单dp
小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...
- UOJ#31 【UR #2】猪猪侠再战括号序列
传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...
- gdoi2017
今年的gdoi第一天t1大水题一道 裸的kmp 但是 我把记录长度的int数组开成了char类型 正解变爆零 心态爆炸......... 后面的第二题两千字题目以及五千字附加故事(我是没有去看,据说全 ...
- Brave Game HDU1846(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 题目: Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电 ...
- 【CC2530入门教程-01】CC2530微控制器开发入门基础
[引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:[1]CC2530微控制器开发入门基础.[2]通用I/O端口的输入和输出.[3]外部中断初步应用. ...
- ASP.NET AjaxControlToolkit-Framework4.0 配置实用(简单介绍CalendarExtender日期控件)
1:下载:AjaxControlToolkit Ajax Control Toolkit .NET 4 Ajax Control Toolkit .NET 4.5 Ajax Control Toolk ...
- Python的异常处理机制 -- (转)
当你的程序中出现异常情况时就需要异常处理.比如当你打开一个不存在的文件时.当你的程序中有一些无效的语句时,Python会提示你有错误存在. 下面是一个拼写错误的例子,print写成了Print.Pyt ...
- Python switch-case语句的实现 -- 字典模拟实现
static void print_asru_status(int status, char *label) { char *msg = NULL; switch (status) { : msg = ...
- hdu 1863 畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others) M ...
- 利用最新Apache解析漏洞(CVE-2017-15715)绕过上传黑名单
转载自:https://www.leavesongs.com/PENETRATION/apache-cve-2017-15715-vulnerability.html 目标环境: 比如,目标存在一个上 ...