POJ1850 组合数学
来源:互联网 发布:复方雄蛾强肾胶囊淘宝 编辑:程序博客网 时间:2024/05/24 05:13
Code
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 8939 Accepted: 4264
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
题意:
字典序为a,b,c,…..ab,输入一个字符串,输出该字符串的位置
题解:
求长度为3满足题意得字符串总个数,假设第一个位置填一个字母,那么就需要再从25个字母里挑两个,也就是C(25,2),假设第一个位置换一个字母,那就再加上C(24,2),如此可知有C(25,2)+C(24,2)……C(2,2)= C(25,3),如此长度小于输入字符串的个数就确定了。接着,我们再求在相同长度时,该字符串排第几。一样的,前面的数确定了,我们挑几个数放进去就好。(因为挑出来的字母肯定只有一种严格递增的排列)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <queue>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;int dp[40][40];char a[40];void init(){ int i,j,k; memset(dp,0,sizeof(dp)); f(i,1,35) dp[i][0] = 1; dp[1][1] = 1; dp[0][0] = 0; f(i,2,35) f(j,1,i) dp[i][j] = dp[i-1][j] + dp[i-1][j-1];}int main(){ init(); while(cin>>a){ int i,j,p = 1,cnt = 1; for(i = 1;a[i]!='\0';i++){ if(a[i]<=a[i-1]){ p = 0; printf("0\n"); return 0; } cnt++; } int ans = 0; if(p){ f(i,1,cnt-1) ans+=dp[26][i]; f(i,0,a[0]-'a'-1) ans+=dp[25-i][cnt-1]; f(i,1,cnt){ f(j,a[i-1]+1-'a',a[i]-1-'a'){ ans += dp[25-j][cnt-(i+1)]; } } } if(p == 0) printf("0\n"); else printf("%d\n",ans+1); } return 0;}
- poj1850(组合数学)
- poj1850 Code 组合数学
- POJ1850-组合数学
- POJ1850 Code组合数学
- POJ1850 组合数学
- poj1850组合数学
- poj1850 组合
- (POJ1850)Code <组合数学,杨辉三角计算每一个组合数nCm>
- POJ1850:Code(组合计数)
- poj1850/poj1496 组合数
- poj1850--Code(组合篇2)
- poj1850
- poj1850
- poj1850
- poj1850
- POJ1850
- poj1850
- poj1850
- Elasticsearch中使用ik分词器 java api
- Utils-date
- Ant自动编译打包&发布
- thinkphp微信扫码支付模式二
- Linux硬链接和软链接(符号链接)
- POJ1850 组合数学
- WMI技术介绍和应用——执行方法
- JavaSE入门学习6:Java基础语法之运算符和语句(上)
- 如何确定高速数据转换系统中存储器DDR的参数以及设计
- ScrollView嵌套ListView只显示一行的解决方法
- 【HDU】 2639 Bone Collector II
- 2016 Winter Training Day #1_F题_codefcrces 507A(贪心)
- <meta>标签属性
- core dump设置(gdb分析)