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. 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]

    骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...

  2. bzoj 2762: [JLOI2011]不等式组——树状数组

    旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了,所以旺喵想挑战旺汪.旺喵给出一组一元不等式,并给出一个数值 .旺汪需要回答 ...

  3. 如何设计一个优雅健壮的Android WebView?(上)

    转:如何设计一个优雅健壮的Android WebView?(上) 前言 Android应用层的开发有几大模块,其中WebView是最重要的模块之一.网上能够搜索到的WebView资料可谓寥寥,Gith ...

  4. Arduino 舵机sg90电位器实现转动方向控制

    /* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...

  5. Tabular DataStream protocol 协议

    Tabular DataStream protocol 协议 Freetds 创建过程 https://wenku.baidu.com/view/2076cbfaaef8941ea76e0576.ht ...

  6. 斯坦福开源无Bug的随机计算图Certigrad

    斯坦福开源无Bug的随机计算图Certigrad https://news.cnblogs.com/n/573690/ ttps://github.com/dselsam/certigrad

  7. squid 代理服务

    squid代理服务分为两种方式: 一.正向代理(用在企业的办公环境中,员工上网需要通过Squid代理来上网) 客户端发送请求到代理服务器,代理服务器去向真正的服务器请求结果,并将结果返回给客户端 二. ...

  8. FineReport——登录不到决策系统

    在不断的测试过程中,可能会造成缓存数据的累积,所以在登录过程中可能会出现登录不到决策系统,而是跳转到某一模板页面 解决方法就是清理缓存或者换一个浏览器测试.

  9. Windows下修改oracle实例不随服务自动启动

    设置Oracle Administration Assistant for Windows 开始->所有程序->Oracle - OraDb10g_home1->配置和移植工具-&g ...

  10. pom报错解决方法大全

    1.Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom 解决方法: Windows: CMD --> c ...