加载中...

POJ 1850 - Code


  • POJ 1850 - Code
  • Time: 1000MS
  • Memory: 30000K
  • 难度: 初级
  • 分类: 排列组合

0x00 POJ 原题

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

Source

Romania OI 2002

0x10 题目概述

与 POJ1496 最猥琐的区别,很多同学只注意到规定 str 的长度不同,以为把 str 数组长度改一下直接复制就能 AC 再多刷一题了,殊不知老是 WA 却找不到原因,大概就是这里出问题了

本题 Str 最长为 10 个字符

0x20 解题思路

组合数学题,(也属 递推数学,是因为杨辉三角和组合数之间的关系)

0x21 判断字符串是否升序

若符合第一步,则首先计算比 str 长度少的所有字符串个数

假设 str 为 vwxyz,则其长度为 5,那么

0x22 统计较短字符串

长度为 2 的字符串,根据开头字母不同,就有 25 种不同情况,编程去处理是很困难的。这里必须要用数学方法去处理。

所以用一个简单的循环就能计算出 比 str 长度少的所有字符串个数 了

这就是数学的威力,把受限的取法转换为不限制的取法

0x23 统计等长且字典序更小的字符串

这个看我程序的注释更容易懂,所以这里就不再啰嗦了,值得注意的是这步我同样利用了公式(1),所以如果看到某些地方取字母的时候看上去好像没有遵守“升序规则”,本来要限制取字母的地方却没有限制,那一定是用公式(1)变换了

0x24 得到字符串的编码

把前面找到的所有字符串的个数之和再 +1,就是 str 的值。

之所以+1,是因为此前的所有操作都只是找 str 之前的字符串,并不包括 str 本身

0x25 计算组合数

最后,剩下一个问题就是怎样得到每一个组合数的值,这个我发现很多同学都是利用打表做的,利用的就是 组合数 与 杨辉三角 的关系(建立一个二维数组 C[n],就能看到他们之间关系密切啊!区别就是顶点的值,杨辉三角为 1,组合数为 0)

其实这个“关系”是有数学公式的:

其实组合数也可以直接用计算方法做(n 的规模可以至少扩展到 1000),不过这里 n 的规模只有 26,打表应该是更快的

0x30 AC 源码

//Memory Time 
// 208K   0MS 

#include<iostream>
#include<string>
using namespace std;

int c[27][27]={0};

/*打表,利用杨辉三角计算每一个组合数nCm*/

void play_table(void)
{
    for(int i=0;i<=26;i++)
        for(int j=0;j<=i;j++)
            if(!j || i==j)
                c[i][j]=1;
            else
                c[i][j]=c[i-1][j-1]+c[i-1][j];
    c[0][0]=0;
    return;
}

int main(int i,int j)
{
    play_table();

    char str[11];
    while(cin>>str)
    {
        int len=strlen(str);

        /*检查str是否符合升序排列*/

        for(i=1;i<len;i++)
            if(str[i-1]>=str[i])
            {
                cout<<0<<endl;   //本题只要求输入一次就允许结束程序
                return 0;        //因此若使用循环输入,一旦str不符合字典要求(如aab,ba等)就要结束程序
            }                    //这是与POJ1496的最隐蔽区别

        int sum=0;  //str的值,初始为0

        /*计算长度比str小的字符串个数*/

        for(i=1;i<len;i++)
            sum+=c[26][i];  //c[26][i]表示 长度为i的字符串的个数

        /*计算长度等于len,但值比str小的字符串个数*/

        for(i=0;i<len;i++)  //i为str的指针,对每一个位置枚举 允许选择的字符ch
        {
            char ch= (!i)?'a':str[i-1]+1;   //ch = str[i-1]+1 根据升序规则,当前位置的ch至少要比str前一位置的字符大1
            while(ch<=str[i]-1)   //ch<=str[i]-1 根据升序规则,当前位置的ch最多只能比 str这个位置实际上的字符 小1
            {
                sum+=c['z'-ch][len-1-i];  //'z'-ch : 小于等于ch的字符不允许再被选择,所以当前能够选择的字符总数为'z'-ch
                ch++;                     //len-1-i  : ch位置后面(不包括ch)剩下的位数,就是从'z'-ch选择len-1-i个字符
            }
        }

        cout<<++sum<<endl;     // 此前的操作都是寻找比str小的所有字符串的个数,并不包括str本身,因此这里要+1
    }
    return 0;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录