题目:

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

题意分析:

对于不同的小写字母,严格升序组成的一组序列,分别代表从1~n的数值。

这题关键是求组合的数值C。对于方法,并不是太难,分两种情况

1.当长度小于给出的字符串L时,对于字符串长度为1的字符串总共代表的数字的量就是C(26,1),对于长度为2的字符串总共代表的数字的量就是C(26,2),一次类推,直到C(26,L-1)。

2.对于长度等于L的,从最开头的字符s[0]开始,对于比这个字符严格小的,如果有一个,我们可以有C('z'-s[0], L-1)个,'z'-s[0] 是因为题意已经说明该字符串每个位置是严格递增的。如果在这个位置,还能找到别s[0]严格小的,则再加上。

再遍历到后面的字符s[i],对于比这个字符严格小的,我们依然可以找到C('z'-s[0], L-i-1)个。一直到最后一个字符,这样,所有结果的总和,就是结果。

对于1中的原理如下:

以两位的串为例,有ab~az,bc~cz,……那么有组合数

递推出这个公式,需要结合组合里的一个很常用的式子

后面的依此类推。有大佬也指出,既然你是升序,那么只要你有几位,你就选几个出来,那么它的严格升序排列只有一种,所以就是C(26,L),就是在长度为L下的所有组合数。

然后利用上面的式子进行递推,求出所有的26以内的所有组合数,再直接利用求结果即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL; LL C[27][27];
char S[15]; void getC()
{
memset(C, 0, sizeof(C));
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] + C[i-1][j-1];
}
}
C[0][0] = 1;
} int main()
{
getC(); while(~scanf("%s", S))
{
LL ans = 0;
int len = strlen(S); //判断升序否
for(int i = 1; i < len; i++)
{
if(S[i] <= S[i-1])
{
printf("0\n");
return 0;
}
} //先把长度比S小的数目都统计加起来
for(int i = 1; i < len; i++)
{
ans += C[26][i];
} //统计长度相同的,枚举相加
for(int i = 0; i < len; i++)
{
int ch = i==0?'a':S[i-1]+1; //考虑到S升序
for(; ch < S[i]; ch++)
{
ans += C['z' - ch][len - i - 1]; //从能选的字母中选出len-i+1个进行组合
}
} ans++; //加自己 printf("%lld\n", ans);
}
return 0;
}

  

POJ_1850 Code【组合的运用】的更多相关文章

  1. oracle 索引,组合索引

    1. 组合索引 id,code      组合 id,number  组合 2. 排序cost 使用 id ,cost=0 使用 id+code  cost=0 使用 id+number  cost= ...

  2. LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)

    题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...

  3. Html 基础介绍 基础标签

    <head> <!-- 设置编码格式 --> <meta charset="UTF-8"> <!-- 设置作者 --> <me ...

  4. C++简介

    本文仅用于学习交流,转载请注明:http://www.cnblogs.com/mxbs/p/6266466.html  Hello,C++ World! 简介: C++融合了3中不同的编程传统:C语言 ...

  5. 键盘事件(keyup、keydown、keypress)

    1.onkeyup 和onkeydown时,keyCode是不区分大小写的,会将小写字母自动转化为大写字母. 2 onkeypress时,区分大小写. 3兼容event.keyCode||event. ...

  6. oracle 语句之对数据库的表名就行模糊查询,对查询结果进行遍历,依次获取每个表名结果中的每个字段(存储过程)

    语句的执行环境是plsql的sql窗口, 语句的目的是从整个数据库中的所有表判断 不等于某个字段的记录数 . 代码如下: declare s_sql clob:=''; -- 声明一个变量,该变量用于 ...

  7. python UI自动化实战记录七:页面2用例编写

    使用python自带的unittest测试框架,用例继承自unittest.TestCase类. 1 引入接口类和页面类 2 setUp函数中打开页面,定义接口对象 3 tearDown函数中关闭页面 ...

  8. 关于CompletableFuture的一切,看这篇文章就够了

    文章目录 CompletableFuture作为Future使用 异步执行code 组合Futures thenApply() 和 thenCompose()的区别 并行执行任务 异常处理 java中 ...

  9. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

随机推荐

  1. Python3.7安装PyQt5的方法

    一.系统环境 操作系统:Win7 64位 Python Version:3.7 二.安装参考 方法1:pip install PyQt5 方法2:下载whl安装包安装 a.下载网址:https://p ...

  2. CORS同源策略

    同源策略以及跨域资源共享在大部分情况下针对的是Ajax请求.同源策略主要限制了通过XMLHttpRequest实现的Ajax请求,如果请求的是一个“异源”地址,浏览器将不允许读取返回的内容. 支持同源 ...

  3. 转Delphi中XLSReadWrite控件的使用(1)---简介

    XLSReadWrite控件简介: 一个你需要的,能在Delphi和.NET下访问Excel文件的完美解决方案. 一个经典的读写Excel的控件,对于使用Excel 开发很有帮助 官方网站: http ...

  4. 备忘录(Memento)模式

    *备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对戏的内部状态, * 并在该对象之外保存这个状态.这样以后就能恢复到原来保存的状态 *Originator(发起人): 负责创建一个备忘 ...

  5. Tomcat调优总结

    Tomcat 优化分为系统优化,Java虚拟机调优,Tomcat本身的优化. Tomcat 如何起停 ./catalina.sh stop ./catalina.sh start/sbin/servi ...

  6. 【转】android中layout_weight的理解

    android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...

  7. 【转】android手势处理揭秘

    当滑动(fling)比移动(scroll)有更高的效率时,为什么要让用户使用scroll操作呢?在面积很小而数据又很多的移动设备上,要显示远在后面的那些内容scroll是很困难的,这种情况下fling ...

  8. Listview 利用Datapager进行分页

    原文:http://lgm9128.blog.163.com/blog/static/421734292010513111851101/ <asp:ListView ID="ListV ...

  9. It企业的上市与退市

    目前我国的it上市公司有同方股份.华胜天成.长城电脑.航天信息.用友软件.中国软件.东软集团.长电科技.华东科技.航天长峰.航天科技.士兰微.上海贝岭等等. 一般来讲公司上市是为了融资,一是为了解决资 ...

  10. .Net Core .Net Core的学习

    .Net Core 学习 一.什么是.net core? 百度百科: https://baike.baidu.com/item/.net%20core/20130686?fr=aladdin 个人总结 ...