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. java爬虫框架jsoup

    1.java爬虫框架的api jsoup:https://www.open-open.com/jsoup/

  2. niceScroll 简单使用 及 插件API

    官方网址[https://nicescroll.areaaperta.com/]  注:效果见官网右侧滚动条 jquery.nicescroll文件下载地址 引入核心文件,插件需要引入1.5.X以上版 ...

  3. MSSQL 错误:在将 varchar 值 '1,2,3,5,6' 转换成数据类型 int 时失败。

    MSSQL  错误:在将 varchar 值 '1,2,3,5,6' 转换成数据类型 int 时失败.

  4. 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'

    使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...

  5. IOException while loading persisted sessions:

    严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException at java. ...

  6. 更改控件中DrawableLeft图片的大小,图片与文字的距离

    Drawable drawable=getResources().getDrawable(R.drawable.xx); //获取图片 drawable.setBounds(left, top, ri ...

  7. java===字符串常用API介绍(转)

    本文转自:http://blog.csdn.net/crazy_kid_hnf/article/details/55102861 字符串基本操作 1.substring(from,end)(含头不含尾 ...

  8. pam_examples

    blank.c /* * $Id$ */ /* Andrew Morgan (morgan@parc.power.net) -- a self contained `blank' * applicat ...

  9. 【bzoj1068】【SCOI2007】压缩

    一道区间dp f[i][j][0/1]表示[i,j]区间是否加入M,并且之前一位有M的最小长度 可以理解为在第一位之前有一个M 那么就可以转移了. #include<bits/stdc++.h& ...

  10. 【bzoj4562】HAOI2016食物链

    记忆化搜索水过去了…… QwQ #include<bits/stdc++.h> #define N 400010 typedef long long ll; using namespace ...