Uva 417 - Word Index 解题报告(组合数)

96 篇文章 0 订阅
该博客介绍了如何解决UVA 417问题,涉及一种简单的编码方案,将特定类型的五字母或更少的小写字母单词编码为整数。博主详细阐述了有效单词的定义、如何根据单词在字母表中的位置计算其索引,以及如何处理无效单词。博客提供了一个解题思路和示例输入输出,强调了计算组合数的方法来确定单词的顺序。
摘要由CSDN通过智能技术生成

 Word Index 

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

    解题报告: 以前在POJ上记得做过相似的题目。在下一般都是比较害怕这种题目的,一般都是思路简单,处理复杂。

    这题经过思考,发现不是那么难搞。首先,长度为1的串一共有C(26, 1)个,长度为2的串一共有C(26, 2)个(一大一小),依次类推。长度为5的串的序数要加上长度为1,2,3,4的串的总数。然后我们分析一下,以长度为3举例,例如dfg。看第一位d,那么我们可以发现首位为a,b,c的串都在它的前面,他们的序数需要加上去,首位为a的长度为3的串有C(25, 2)个,b的是C(24,2)个,c的是C(23,2)个。再看第二位f,在它前面的只有e了,类似处理,有C(21,1)个。这样加起来再加1就是答案了。另外判断一下当前位是否大于上一位,否则直接输出0。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int C[27][6];
char str[6];

void init()
{
    for(int i=0;i<=26;i++)
    {
        C[i][0]=1;
        for(int j=1;j<=5;j++)
        {
            C[i][j] = C[i-1][j]+C[i-1][j-1];
        }
    }
}

void work()
{
    int ans = 1;

    int len = strlen(str);
    for(int i=1;i<len;i++) if(str[i]<=str[i-1])
    {
        puts("0");
        return;
    }

    for(int i=1;i<len;i++)
        ans += C[26][i];

    char ch='a';
    for(int i=0;i<len;i++)
    {
        for(;ch<str[i];ch++)
        {
            ans+=C['z'-ch][len-i-1];
        }
        ch++;
    }

    printf("%d\n", ans);
}

int main()
{
    init();
    while(~scanf("%s", str))
        work();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值