poj1850 Code
来源:互联网 发布:php jenkins docker 编辑:程序博客网 时间:2024/06/17 17:31
Description
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 word is maximum 10 letters length
• The English alphabet has 26 characters.
Output
Sample Input
bf
Sample Output
55
大致题意: 给定一个字符串,问这个字符串按照给定规律的位置。
规律:字符串<=10,(s[i-1] < s[i]),如果存在s[i-1] >= s[i],则输出0
解题思路:找出规律就容易解答了,当有一个字符时, ans = 26;
当有两个字符时a(b-z) = 25, b(c-z) = 24, c(d-z) = 23, ... ,yz = 1.
= c(25,1) + c(24,1) + ... + c(n, 1) + c(n-1,1) + c(1,1)
因为 c(n,m) = c(n-1,m-1) +c(n-1,m);
c(n-1,m) = c(n-2, m-1) + c(n-2, m);
c(n-2,m) = c(n-3, m-1) + c(n-3, m);
...
c(m+1, m) = c(m, m-1) + c(m, m) = c(m, 1) + 1;
c(m, m) = 1;
所以 c(n, m) = c(n-1, m-1) + c(n-2, m-1) + ... + c(m, m-1) + 1;
c(n, m) =c(n-1, m-1) + c(n-2, m-1) + ... + c(2, m-1) + c(1, m-1);
(c(s,t) = 0 t>s);
所以两个字符为c(26,2), 同理 3个 c(26,3) ... 10个 c(26, 10);
例如给bfgi = 1.[len<4的所有] + 2.[len = 4(小于 bfgi)];
1. 将所有len<4的c(26,i) 和计算;
2. 就要枚举<bfgi(长度相等的字符串), 如:a..., (ba.. to be..), (bfg. to bfgh)
也就是为a时 c('z' - 'a', len-pos(a)-1), 为什么是('z' - 'a'),
因为a下一个,从b-z,len-pos(a)-1就是除了a余下的长度,同理可得余下的。
代码:
#include <stdio.h>#include <string.h>int sum;int c[27][27] = {0};char str[20];void init(){int i, j;for (i=0; i<27; ++i){for (j=0; j<=26; ++j){if (!j || i==j)c[i][j] = 1;elsec[i][j] = c[i-1][j-1] + c[i-1][j];}}}void solve(){int len = strlen(str);sum = 0;int i, j;for (i=1; i<len; ++i)sum += c[26][i];char ch;for (i=1; i<len; ++i){if (str[i-1] >= str[i]){puts("0");return;}}for (i=0; i<len; ++i){ch = i==0?'a':str[i-1]+1;while (ch < str[i]){sum += c['z' - ch][len-i-1];ch++;}}sum++;printf("%d\n", sum);}int main(){init();while (~scanf("%s", str)){solve();}return 0;}
- POJ1850-Code
- poj1850 Code
- poj1850 Code
- poj1850 Code
- POJ1850:Code(组合计数)
- poj1850 Code 组合数学
- POJ1850 Code 【排列组合】
- POJ1850 Code组合数学
- POJ1850——Code
- POJ1850 Code【全排列】
- POJ1850——Code
- poj1850 Code (瞎搞)
- POJ1850——Code
- poj1850 code 字串的编号
- poj1850--Code(组合篇2)
- poj1850
- poj1850
- poj1850
- 操作系统原理
- RT73(RT2571WF)(TL-WN321G+) USB无线WIFI移植S3C2440+linux2.6.30.4
- Extjs4.2.1 Ext.grid.plugin.RowEditing 保存按钮不可用
- c++ 随机数
- FI--SAP财务成本知识库
- poj1850 Code
- Android APP --- Mr Expense
- iphone开发资源汇总
- rspec BDD(行为驱动开发 敏捷开发模型)的一种插件工具
- #ifdef电脑程序语句,我们可以用它区隔一些与特定头文件、程序库和其他文件版本有关的代码。
- Tomcat安装与配置
- Apache Mesos:数据中心就是计算机
- 自己电脑发布网站到外网实现过程
- 扩展元数据