Codeforces Gym-101291A Alphabet

http://codeforces.com/gym/101291/attachments

Alphabet

A string of lowercase letters is called alphabetical if deleting zero or more of its letters can result inthe alphabet string “abcdefghijklmnopqrstuvwxyz”.

Given a string s, determine the minimum number of letters to insert anywhere in the string tomake 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 beinserted in order to make the string s alphabetical.

Sample Input

xyzabcdefghijklmnopqrstuvw 

Sample Output

3

Sample Input

aiemckgobjfndlhp

Sample Output

 20

求最长上升子序列个数,然后用26减去它

#include 
#include 
int main()
{
    char a[55];
    int b[55];
    int n,i,j,c,d;
    scanf("%s",a);
    n=strlen(a);
    b[0]=1;
    for(i=1; i<n; i++)
    {
        c=0;
        for(j=0; j<i; j++)
        {
            if(a[j]<a[i]&&c<b[j])
                c=b[j];
        }
        b[i]=c+1;
    }
    d=-1;
    for(i=0; i<n; i++)
    {
        if(d<b[i])
            d=b[i];
    }
    printf("%d",26-d);
    return 0;
}

你可能感兴趣的:(Codeforces Gym-101291A Alphabet)