Code--POJ1850

Description

Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that the words are made only of small characters of the English alphabet a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).
The coding system works like this:
• The words are arranged in the increasing order of their length. • The words with the same length are arranged in lexicographical order (the order from the dictionary). • We codify these words by their numbering, starting with a, as follows:
a - 1
b - 2
...
z - 26
ab - 27
...
az - 51
bc - 52
...
vwxyz - 83681
...
Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.

Input

The only line contains a word. There are some constraints: • The word is maximum 10 letters length • The English alphabet has 26 characters.

Output

The output will contain the code of the given word, or 0 if the word can not be codified.

Sample Input

bf

Sample Output

55


题意显而易见;看代码吧

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define maxn 30

int main()
{
    long long i, j, k, a[maxn][maxn]={0}, b[maxn]={0,26};//a[3][2]代表长度3(i),以b(j)开头的的有多少个
    char s[20];

    for(i=1; i<=26; i++)
        a[1][i] = 1;

    for(i=2; i<=26; i++)
    {
        for(j=1; j<=26; j++)
        {
            for(k=j+1; k<=26; k++)
                a[i][j] += a[i-1][k];
            b[i] += a[i][j];
        }
    }

    while(scanf("%s", s) != EOF)
    {
        int len = strlen(s), ok=0;
        long long sum = 0;

        for(i=1; i<len; i++)
            sum += b[i];

        for(i=1;i<s[0]-'a'+1;i++)
            sum+=a[len][i];
        len--;
        for(i=1; s[i]; i++)
        {
            if( s[i] <= s[i-1])
                ok = 1;
            for(j=s[i-1]-'a'+2;j<s[i]-'a'+1;j++)
            {
                sum+=a[len][j];
            }
            len--;
        }

        if(ok)
            printf("0\n");
        else
            printf("%lld\n", sum+1);
    }

    return 0;
}

 

posted @ 2015-04-29 20:35  西瓜不懂柠檬的酸  Views(160)  Comments(0Edit  收藏  举报
levels of contents