回 帖 发 新 帖 刷新版面

主题:[讨论]Word Index

Word Index

Time Limit: 1 Seconds     Memory Limit: 32768 K

Total Submit:9     Accepted:6

--------------------------------------------------------------------------------

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

回复列表 (共3个回复)

沙发

#include<stdio.h> 
#include<string.h> 
int main() 

int i, j, k, l, m, count, h; 
char str[6], store[200000][6]; 
count = 1; 
for(i='a'; i<='z'; i++) 

str[0] = i; 
str[1] = '\0'; 
strcpy(store[count++], str); 

for(i='a'; i<='z'; i++) 
for(j=i+1; j<='z'; j++) 

h=0; 
str[h++] = i; 
str[h++] = j; 
str[h] = '\0'; 
strcpy(store[count++], str); 


for(i='a'; i<='z'; i++) 
for(j=i+1; j<='z'; j++) 
for(k=j+1; k<='z'; k++) 

h=0; 
str[h++] = i; 
str[h++] = j; 
str[h++] = k; 
str[h] = '\0'; 
strcpy(store[count++], str); 


for(i='a'; i<='z'; i++) 
for(j=i+1; j<='z'; j++) 
for(k=j+1; k<='z'; k++) 
for(l=k+1; l<='z'; l++) 

h=0; 
str[h++] = i; 
str[h++] = j; 
str[h++] = k; 
str[h++] = l; 
str[h] = '\0'; 
strcpy(store[count++], str); 


for(i='a'; i<='z'; i++) 
for(j=i+1; j<='z'; j++) 
for(k=j+1; k<='z'; k++) 
for(l=k+1; l<='z'; l++) 
for(m=l+1; m<='z'; m++) 

h=0; 
str[h++] = i; 
str[h++] = j; 
str[h++] = k; 
str[h++] = l; 
str[h++] = m; 
str[h] = '\0'; 
strcpy(store[count++], str); 


while(scanf("%s", str) != EOF) 

for(i=1; i<count; i++) 

if(strcmp(store[i], str) == 0) 

printf("%d\n", i); 
break; 


if(i == count) printf("0\n"); 

return 0; 

板凳

#include<stdio.h>
#include<string.h>
long com(int x,int y)
{
    long sum=1;
    int i;
    for(i=0;i<y;i++)
    {
        sum*=(x-i);
    }
    for(;y;y--) sum/=y;
    return sum;
}
int main()
{
    char s[8];
    long n;
    int i,j;
    while(scanf("%s",s)!=EOF)
    {
        for(i=0;s[i+1];i++) 
        {
            if(s[i]>=s[i+1]) break;
        }
        if(s[i+1]) printf("0\n");
        else
        {
            i=strlen(s);
            for(--i,n=0;i;i--) 
                n+=com(26,i);
            for(i=0;s[i];i++)
            {
                if(i==0)j='a';
                else j=s[i-1]+1;
                for(;j<s[i];j++)
                {
                    n+=com('z'-j,strlen(s)-i-1);
                }
            }
            printf("%ld\n",n+1);
        }
    }
    return 0;
}

这个还算简单的.PKU上还有一个一样的题目但是数据规模变大了.
http://acm.pku.edu.cn/JudgeOnline/problem?id=1850

3 楼

pku的1850需要对com做如下的改动:
long com(int x,int y)
{
    long sum=1;
    int i,j;
    for(i=0,j=1;i<y;i++)
    {
        sum*=(x-i);
        for(;j<=y&&sum%j==0;j++) sum/=j;
    }
    return sum;
}
或者简单一点,时间可能就要多一点
long com(int x,int y)
{
    if(x==y||y==0) return 1;
    else return com(x-1,y)+com(x-1,y-1);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册