组合数学poj 1496 1850 同样的代码过两题

Description   1942

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

1496

Description

Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers. 

Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example, 

abc aep gwz 

are all valid three-letter words, whereas 

aab are cat 

are not. 

For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is: 

a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681

Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long. For each word read, if the word is invalid give the number 0. If the word read is valid, give the word's position index in the above alphabetical list. 

Input

The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line. 

The input will be terminated by end-of-file.

Output

The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.

Sample Input

z
a
cat
vwxyz

Sample Output

26
1
0
83681
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int ms=26;
int c[ms+1][ms+1];
int main()
{
    int i,j;
    //bool flag;
    for(i=0;i<=ms;i++)
    {
        c[i][0]=c[i][i]=1;
    }
    for(i=2;i<=ms;i++)
      for(j=1;j<i;j++)
        c[i][j]=c[i-1][j]+c[i-1][j-1];
    char str[ms];
    while(scanf("%s",str)!=EOF)
    {
        bool flag=false;
        int len=strlen(str);
        for(i=1;i<len;i++)
           if(str[i]<=str[i-1])
            {
                flag=true;
                break;
            }
        if(flag)
        {
            printf("0
");
            continue;
        }
        int ans=0;
        i=1;
        while(i<len)
           ans+=c[26][i++];
        i=-1;
        int k=0;
        while(k<len)
        {
            i++;
            if(k==len-1)
            {
                //if(i+'a'<=str[k])
                while(i+'a'<=str[k]) 
                {
                    ans++;
                    i++;
                }
                break;
            }
            while(i+'a'<str[k])
            {
                  //if(i+'a'<str[k])
                 //{
                ans+=c[26-i-1][len-1-k];
                i++;
                continue;
            
            }
        k++;
        }
        printf("%d
",ans);
    } 
    return 0;
}


原文地址:https://www.cnblogs.com/767355675hutaishi/p/3824257.html