Fork me on GitHub

SCAU 2006校赛 6582 Encode

6582 Encode

时间限制:753MS  内存限制:1000K 提交次数:48 通过次数:25

题型: 编程题   语言: 无限制

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.

输入格式

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.

输出格式

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.

输入样例

z
a
cat
vwxyz

输出样例

26
1
0
83681

作者

admin

#include<stdio.h>
#include<string.h>
int main()
{
    int len, i, j, sum, ans, flag, a1, a2, a3, a4, a5, t, temp1, temp2, temp3, temp4;
    char word[8];
    int base[4];//保存长度分别为1,2,3,4 时的最后一串字符所代表的值,即z, yz, xyz, wxyz,为了方便下一长度的字符串调用 
    memset(base, 0, sizeof(base));
    base[0] = 26;
    base[1] = base[0] + (25+1)*25/2;
    for(i=24; i>=1; --i) base[2] += (1+i)*i/2;
    base[2] += base[1];
    for(i=23; i>=1; --i)
    {
        base[3] += (24-i)*(i+1)*i/2;
    }
    base[3] += base[2];
    while(scanf("%s", word) != EOF)
    {
        len = strlen(word);
        for(i=0,flag=0; i<len-1; ++i)
            {
                for(j=i+1; j<len && word[i] < word[j]; ++j);
                if(j < len) {flag = 1; break;}
            }
        if(flag)
        {
            printf("0\n");
            continue;
        }
        else
        {
            sum = 0;
            switch(len)
            {
                case 1 :  
                        sum = word[0] - 'a' + 1;
                        break;
                case 2 :
                        a1 = word[0] - 'a';
                        for(i=0,j=25; i<a1; ++i, j--) sum += j;
                        sum += base[0];
                        a2 = word[1] - 'a' - a1;
                        sum += a2;
                        break;
                case 3 :
                        a1 = word[0] - 'a';
                        for(i=0; i<a1; ++i)
                         sum += (1+24-i)*(24-i)/2;
                         a2 = word[1] - 'a' - a1;
                          for(i=1; i<=a2-1; ++i)
                           sum += (24-a1-(i-1));
                           sum += base[1];
                           a3 = word[2] - 'a' - (word[1] - 'a');
                           sum += a3;
                           break;
                  case 4 :
                          a1 = word[0] - 'a';
                          sum += base[2];
                          for(i=1; i<=a1; ++i)
                          {
                              for(j=24-i; j>=1; --j)
                               sum += (j+1)*j/2;
                         }
                         temp1 = 23 - a1;
                         a2 = word[1] - 'a' - a1;
                         for(i=1; i<=a2-1; ++i)
                              sum += (temp1+1-i+1)*(temp1+1-i)/2;
                             a3 = word[2] - 'a' - (word[1] - 'a');
                             temp2 = temp1 - a2 + 1;
                             for(i=0; i<a3-1; ++i)
                              sum += temp2-i;
                            a4 = word[3] - 'a' - (word[2] - 'a');
                             sum += a4;
                             break;
                  case 5 :
                          a1 = word[0] - 'a';
                          sum += base[3];
                          for(i=1; i<=a1; ++i)
                          {
                              for(j=23-i; j>=1; --j)
                                  sum += (24-i-j)*(j+1)*j/2;
                          }
                          temp1 = 22 - a1;
                          a2 = word[1] - 'a' - a1 - 1;
                          for(i=1; i<=a2; ++i)
                              for(j=temp1+1-i; j>=1; --j)
                                  sum += (j+1)*j/2;
                                  
                          a3 = word[2] - 'a' - (word[1] - 'a');
                          temp2 = temp1 - a2;
                          for(i=1; i<=a3-1; ++i)
                              sum += (temp2+1-i+1)*(temp2+1-i)/2;
                          a4 = word[3] - 'a' - (word[2] - 'a') - 1;
                          temp3 = temp2 - a3 + 1;
                          for(i=0; i<a4-1; ++i)
                              sum += (temp3-i);
                          a5 = word[4] - 'a' - (word[3] - 'a');
                          sum += a5;
                          break;
            }
            printf("%d\n", sum);
        }
            
    }
    return 0;
}

posted @ 2013-03-31 11:29  Gifur  阅读(316)  评论(0编辑  收藏  举报
TOP