题目:

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;
}

  

最新文章

  1. Eclipse使用Maven tomcat:run命令启动web项目时修改默认端口
  2. SQLite剖析之临时文件、内存数据库
  3. php大力力 [044节] PHP的POST语句一定要大写!!if(!empty($_POST[&#39;id&#39;])) {
  4. px和em区别-在font-size的 css 的使用
  5. WCF自定义地址路由映射(不用svc文件)
  6. C#占位符与格式化字符串
  7. Diving Into Lync Client Logins
  8. iptables的CLUSTER target以太网交换机和想法
  9. MFC控件(8):command button与syslink control
  10. Linux下制作静(动)态库
  11. JQuery --- 第三期 (jQuery事件相关)
  12. angular6 开发实践基础知识汇总
  13. Linux下的进程结构
  14. 任务调度工具 Apache Airflow 初识
  15. 编译GSLSDevil的全过程
  16. 【ML】概率图模型
  17. MAC里“微软雅黑”字体标准体和粗体无法同时使用问题的解决方法
  18. kali linux之DNS,NTP放大攻击
  19. vs2013数据库连接对应的dll
  20. poj3709 K-Anonymous Sequence[贪心+斜率优化dp]

热门文章

  1. 面试题--CVTE
  2. Ubuntu下配置Apache的Worker模式
  3. 通过event事件来控制红绿灯通行车辆
  4. HttpRuntime.Cache
  5. Row_Number() OVER()函数使用举例
  6. How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
  7. 编写高质量代码改善C#程序的157个建议——建议38:小心闭包中的陷阱
  8. Fragment生命周期及在viewpager中的生命周期
  9. delphi TString使用(取有规律的字符串中某一项内容)
  10. JS和JQuery的比较