c++ A - Alphabet Gym - 101291A SDUT

在这里插入图片描述
A string of lowercase letters is called alphabetical if deleting zero or more of its letters can result in
the alphabet string “abcdefghijklmnopqrstuvwxyz”.
Given a string s, determine the minimum number of letters to insert anywhere in the string to
make it alphabetical.

Input

The input consists of a single line containing the string s (1 ≤ |s| ≤ 50).
It is guaranteed that s consists 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 be
inserted in order to make the string s alphabetical.

Sample InputSample Output
xyzabcdefghijklmnopqrstuvw3
Sample InputSample Output
aiemckgobjfndlhp20

题目大意:给出一个只用小写字母的字符串,求出想字符串中插入最少的字符后能让字符串中出现 “abcdefghijklmnopqrstuvwxyz”.
解题思路:
在26个连续的字母序列中ascll值是从97按照梯度为1逐渐递增,所以这道题其实是求出字符串的ascll值得最长上升子序列。

// Alphabet.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a[52],len[52];//储存字符串的ascll值和最长上升子序列
    string str;
    getline(cin, str);//输入字符串
    for (int i = 0; str[i]; i++)//将字符串的ascll赋给a数组
    {
        a[i] = str[i];
        len[i] = 1; //先初始化最长上升子序列的长度为1;
    }
    string::size_type lenght = str.size();//字符串的长度
	
	//按照动态规划的思想求出最长上升子序列
    for (int i = 0; i < lenght; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (a[i] > a[j])
                len[i] = max(len[i], len[j] + 1);
        }
    }

    int Max = 0;//找出上升子序列的最长长度
    for (int i = 0; i < lenght; i++)
    {
        if (Max < len[i])
            Max = len[i];
    }
    cout << 26 - Max << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值