组合数学。。。。

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. Oracle 查询今天、昨日、本周、本月和本季度的所有记录

    Oracle 查询今日.昨日.本周.本月和本季度的所有记录 字段类型为date 今日 select * from 表名 where to_char(字段名,'dd')=to_char(sysdate, ...

  2. iOS 自定义对象转NSDictionary

    我们在向后台Post数据的时候,常常需要把某个对象作为参数,比如在AF的框架中,我们进行Post时,其中的para参数就是需要NSdictionary的 Alamofire.request(.POST ...

  3. 用jQuery调用微信api生成二维码

    其实这个,也没什么实际用途,只能测试一下api能不能用. 1. 用Chrome打开一个https://api.weixin.qq.com/页面,会返回一个错误信息,忽略不管,F12打开控制台 2. 控 ...

  4. SQLite遇到的关于x64、x86问题

    初次使用SQLite遇到了莫名其妙的问题: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.92.0, Culture=neutral, PublicKeyTok ...

  5. PTA fzu_oop_east

    GitHub链接: 传送门 5-1该日是该年的第几天 定义一个日期类Date,内有数据成员年.月.日,另有成员函数:构造函数用于初始化数据成员,输出,闰年的判断. 编写主函数:创建日期对象,计算并输出 ...

  6. HTML之:让网页中的<a>标签属性统一设置-如‘新窗口打开’

    在开发过程中,我们往往想在页面中,给<a>设置一个统一的默认格式,例如我们想让链接:“在新窗口打开”,我们就可以使用<base>标签 在网页中添加这段代码: <head& ...

  7. WPF资源字典

    如果相同的资源可用于不同的应用程序,把资源放在一个资源字典中就比较有效. 新建一个资源字典文件Dictionary1.xaml <ResourceDictionary xmlns="h ...

  8. 软件工程个人作业 - week1

    <构建之法>阅读疑惑: 如何寻找开发效率和性能的均衡点?显然开发效率强调封装,重视代码重用.但是遗憾的是代码重用往往泛化了数据特征,降低了效率. 如何“公平”分配工作?尤其是团队中人员参差 ...

  9. js021-Ajax与Comet

    js021-Ajax与Comet 本章内容: 使用XMLHttpRequet对象 使用XMLHttpRequet事件 跨域Ajax通信的限制 Ajax技术的核心是XMLHttpRequet对象 21. ...

  10. CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡

    一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2.6.32-279.el6.i686 LVS版本:ipvsadm-1.26 keepalive ...