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的更多相关文章

  1. 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...

  2. 2021.12.07 [TJOI2013]最长上升子序列(Treap+DP)

    2021.12.07 [TJOI2013]最长上升子序列(Treap+DP) https://www.luogu.com.cn/problem/P4309 题意: 给定一个序列,初始为空.现在我们将1 ...

  3. 2016 China Collegiate Programming Contest Final

    2016 China Collegiate Programming Contest Final Table of Contents 2016 China Collegiate Programming ...

  4. 2016级算法第四次上机-F.AlvinZH的最“长”公共子序列

    940 AlvinZH的最"长"公共子序列 思路 DP,难题. \(dp[i][j]\) :记录A的前i个字符与B的前j个字符变成相同需要的最小操作数. 初始化:dp[i][0] ...

  5. 简单Dp----最长公共子序列,DAG最长路,简单区间DP等

    /* uva 111 * 题意: * 顺序有变化的最长公共子序列: * 模板: */ #include<iostream> #include<cstdio> #include& ...

  6. UVA10599:Robots(II)(最长上升子序列)

    Your company provides robots that can be used to pick up litter from fields after sporting events an ...

  7. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

随机推荐

  1. 【hdu1255】线段树求矩形面积交

    题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...

  2. [Unity]游戏Inside中的Chromatic Aberration效果学习

    Chromatic Aberration效果指的是模拟摄像机的拍摄瑕疵导致rgb三个通道的颜色发生了偏移,如 传统的Chromatic Aberration实现往往是基于一个后处理,将rgb采样的坐标 ...

  3. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  4. js常用模板引擎

    baiduTemplate(百度).artTemplate(腾讯).juicer(淘宝).xtemplate.doT.Jade 1.Handlebars 是 JavaScript 一个语义模板库,通过 ...

  5. CentOS7修改默认运行级别

    新装了一个虚拟机,图形界面启动太慢,想调整一下按照以前的经验改运行级别,输入: vi /etc/inittab 然后发现跟之前不一样了,在inittab设置不再生效: # inittab is no ...

  6. python模块(requests,logging)

    一.requests Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythone ...

  7. python基础===* 解包,格式化输出和print的一点知识

    python3中的特性: >>> name = "botoo" >>> print(f"my name is {name}" ...

  8. 通过call_usermodehelper()在内核态执行用户程序【转】

    转自:http://edsionte.com/techblog/archives/category/linux%E5%86%85%E6%A0%B8%E7%BC%96%E7%A8%8B 背景 如何在Li ...

  9. PhysX SDK src

    PhysX SDK src Physx3.3 source code http://download.csdn.net/download/qq122252656/9427387 Nvidia CUDA ...

  10. 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using ...