本文主要是介绍Alphabet,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Alphabet
题目描述
A string of lowercase letters is called alphabetical if some of the letters can be deleted so that the only letters that remain are the letters from `a' to `z' in order.
Given a string s, determine the minimum number of letters to add anywhere in the string to make it alphabetical.
Given a string s, determine the minimum number of letters to add anywhere in the string to make it alphabetical.
输入
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The only line of input contains a string s (1≤|s|≤50) which contains only lowercase letters.
输出
Output a single integer, which is the smallest number of letters needed to add to s to make it alphabetical.
样例输入
xyzabcdefghijklmnopqrstuvw
样例输出
3
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
char str[55];
int dp[55];
int main(){while(~scanf("%s",str+1)){memset(dp,0,sizeof(dp));int n=strlen(str+1);for(int i=1;i<=n;i++)for(int j=0;j<=i-1;j++)if(str[i]>str[j])dp[i]=max(dp[i],dp[j]+1);int ans=0;for(int i=1;i<=n;i++)ans=max(ans,dp[i]);cout<<26-ans<<endl;}return 0;
}
这篇关于Alphabet的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!