POJ 1850 Code (递推数学)

第一次接触组合数学。。
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
首先要知道题目的意思 两个字符串比较 无论长度只要有一个对位的字母比它小,那么这个字符串就是小的,
所以要得到这个字符串是在第几个位置,就要得到前面所有字母排列的数量,
要求得前面得数量就要用到杨辉三角 因为规律一样
http://www.cnblogs.com/ZhaoPengkinghold/p/4002669.html
借用这个大神的话
解题思路:

排列组合,记住公式:
![这里写图片描述](https://img-blog.csdn.net/20161222164118273?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGl0dGxld2hpdGU1MjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

先看字符的长度一个字符的时候是26为C(26,1),两个字符的时候分别从以a开头的计算出25个,b开头的有24个…可以计算出C(26,2) 依次三个字符的时候C(26,3)….接下来的看代码解释吧

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
using namespace std;
int c[30][30];
int zuhe()
{
    int i,j;
    for(i=0;i<30;i++)
    {
        c[i][0]=1;
        c[i][i]=1;
        for(j=1;j<i;j++)
        {
            c[i][j]=c[i-1][j]+c[i-1][j-1];   //很抽象,其实杨辉三角的总体性质,要求杨辉三角里面一个数,
        }                                  //可以把这个数化成两个数一直化。恰好和这题要求的规律一致 能求得结果,根据公式1.
                          //列坐标代表的是字符串还剩下多少长度。所以列坐标不可能大于行坐标。
                          //这里的意思就是还有26个字母没排,要把26个字母排在i个位置,而且是按规律的 规律是公式1
                        //从1开始得到结果 第0行是构造的基底 
    }
}
int main()
{
    char s[15];
    zuhe();                   //打出杨辉三角的表
    while(scanf("%s",s)!=EOF)
    {
        int sum=0;
        int len=strlen(s);
        int i;
        int ce=0;
        for(i=0;i<len-1;i++)
        {
            if(s[i+1]<s[i])
                ce=1;
        }
        if(ce)
        {
                printf("0\n");
                continue;         //如果有降序,退出
        }
        for(i=1;i<len;i++)   //排小于等于所有
        {
            sum+=c[26][i]; //杨辉三角里面,行坐标代表的是还有多少个字母可以排,
                          //列坐标代表的是字符串还剩下多少长度。所以列坐标不可能大于行坐标。
                          //这里的意思就是还有26个字母没排,要把26个字母排在i个位置,而且是按规律的 规律是公式1
                          //0列0行最后一列都是构造的基底。 
        }                 

        for(i=0;i<len;i++)
        {
            char ch=((!i)?'a':(s[i-1]+1));
            while(ch<s[i])                //这里的长度肯定比26小
            {
                sum+=c['z'-ch][len-1-i];  //由于ch比s[i]小,所以求当s[i]位为ch的时候所有位的全排列。
                                          //而当s[i]位为ch的时候 前面所有位的字母已经定了,所以只能求大于当前ch的所有字母
                                         //而且按规则的全排列。杨辉三角里面,行坐标代表的是还有多少个字母可以排,
                                         //列坐标代表的是字符串还剩下多少长度。所以列坐标不可能大于行坐标。
                ch++;
            }
        }
        printf("%d\n",++sum );

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值