Problem M — limit 1 second Alphabet
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 Input Sample Output
xyzabcdefghijklmnopqrstuvw 3
Sample Input Sample Output
aiemckgobjfndlhp 20
2016 Pacific Northwest Region Programming Contest—Division 2
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
# define N 1000
int str[N+2][N+2];
int main()
{
int i,j,la,lb;
char str1[N+1]="abcdefghijklmnopqrstuvwxyz";
char str2[N+1];
scanf("%s",str2);
la=strlen(str1);
lb=strlen(str2);
for(i=0;i<=la;i++)
str[0][i]=0;
for(j=0;j<=lb;j++)
str[j][0]=0;
for(j=0;j<la;j++)//枚举断点
for(i=0;i<lb;i++)
{
if(str1[j]==str2[i]) str[i+1][j+1]=str[i][j]+1;
else str[i+1][j+1]=max(str[i][j+1],str[i+1][j]);
}
printf("%d\n",26-str[lb][la]);
return 0;
}