组合数学。。。。

Code
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7202 Accepted: 3361

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

主要是这个公式。。。

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

所以用一个简单的循环就能计算出 比str长度少的所有字符串个数 了,这就是数学的威力,把受限的取法转换为不限制的取法

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int c[30][30];
char str[30];

void Init()
{
    for(int i=0;i<30;i++)
        c[0]=c=1;
    for(int i=2;i<30;i++)
        for(int j=1;j<i;j++)
            c[j]=c[i-1][j-1]+c[i-1][j];
}

int main()
{
    Init();
    while(scanf("%s",str)!=EOF)
    {
        bool flag=true;
        int len=strlen(str);
        for(int i=0;i<len-1;i++)
        {
            if(str>=str[i+1])
            {
                flag=false;
                break;
            }
        }
        if(!flag) { printf("0\n");continue; }

int sum=0;
        ///n-1...1
        for(int i=1;i<len;i++)
        {
            sum+=c[26];
        }
        ///N
        for(int i=0;i<len;i++)
        {
            char ch=(i==0)?'a':str[i-1]+1;
            while(ch<str)
            {
                sum+=c['z'-ch][len-1-i];
                ch++;
            }
        }
        printf("%d\n",++sum);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 1850 Code的更多相关文章

  1. POJ 1850 Code(组合数)

    http://poj.org/problem?id=1850 题意 :给定字符串,系统是用字符串组成的,字符串是按字典序排的.编码系统有三条规则,1这些的单词的长度是由小到大的,2相同长度的按字母在字 ...

  2. poj 1850 code(组合数学)

    题目:http://poj.org/problem?id=1850 题意:按给定的规则给字母编号. 一个很简单的题目,但是却做了好久.................................. ...

  3. POJ 1850 Code(找规律)

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7913   Accepted: 3709 Description ...

  4. POJ 1850 Code 字符串 难度:1

    题意: 1 如果是严格升序的字母字符串,那么可以输出非0解码,否则不能译码输出0 2 字符串解码 遵循递增原则,其值为 到现在为止的所有按字母序小于该字符串的数量 + 1; #include < ...

  5. POJ - 1850 Code(组合数学)

    https://vjudge.net/problem/POJ-1850 题意 输出某字符串在字典中的位置.字符串不合规则时输出0. 分析 首先判断字符串合法性,也就是判断是不是升序排列的.如果符合,以 ...

  6. poj:1850 Code(组合数学?数位dp!)

    题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...

  7. 【POJ 1850】 Code

    [POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...

  8. POJ 1496 POJ 1850 组合计数

    Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8256 Accepted: 3906 Description Tran ...

  9. POJ - 1850 B - Code

    Transmitting and memorizing information is a task that requires different coding systems for the bes ...

随机推荐

  1. 开发新产品的三个验证阶段(EVT/DVT/PVT)

    1.EVT, Engineering Validation Test 是针对工程原型机的验证,对象很可能是一大块开发板,或是很多块开发板:关键是要有足够时间和样品. 通常,如果是新平台,需要花的时间和 ...

  2. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 3 One-sample and two-sample tests

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  3. 《JavaScript权威指南》学习笔记 第一天。

    这是零零散散的笔记,作为自己看书打demo的笔记.不足为各位学习,留作自己复习知识点备用. 1.检测对象中某个属性存在不存在: <script> // in 运算符 //不管是对象的自有属 ...

  4. w3m常用快捷键

    H    显示帮助 q    退出,会有提示的 j,k,l,h  移动光标 J/K   向下/向上滚屏 T     打开一个新标签页 Esc-t 打开所有标签页,供你选择,使用jk来上下移动 U    ...

  5. wpf comboBox取值问题

    这是获取值后台代码 private void button1_Click(object sender, RoutedEventArgs e)        {            combBox = ...

  6. 在JavaScript中,arguments是对象的一个特殊属性。

    arguments对象 function函数的内置参数的"数组"/"集合":同时arguments对象就像数组,但是它却不是数组. 常用属性: 1.length ...

  7. Java数据库——事务处理

    在数据库中执行5条SQL语句,这些SQL语句本身需要保持一致,即要么同时成功,要么同时失败 事务基本操作 //============================================= ...

  8. LyX-220-Installer-3

    所见即所得 单独安装这个写作业可以了,要发论文用CTeX Ctrl + M 打开数学输入,里面可以输入 TeX 代码

  9. MySQL取每组的前N条记录

    一.对分组的记录取前N条记录:例子:取前 2条最大(小)的记录 .用子查询: SELECT * FROM right2 a WHERE > (SELECT COUNT(*) FROM right ...

  10. 无法访问 IIS 元数据库。您没有足够的特权访问计算机上的 IIS 网站

    我是WIN10+VS2015  之前用的是VS2012     后来卸载了VS2012 就出现了这个错误,请问该如何解决 在VS的图标上 按右键用管理员(Administrator)运行