POJ1850——Code(组合数学)

Code

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

题目大意:

    判断一个字符串在字典中的位置。

    字符串必须保证为升序字符串才能合法。

解题思路:

    组合数学问题。

    首先通过dp打印出来com[][]杨辉三角形。

    假设字符串的长度为len。

    详情见备注。

Code:

 1 /*************************************************************************
 2     > File Name: poj1850.cpp
 3     > Author: Enumz
 4     > Mail: [email protected]
 5     > Created Time: 2014年10月28日 星期二 01时32分11秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 100000
23 using namespace std;
24 long long com[30][30];
25 void init()   /*打印杨辉三角*/
26 {
27     for (int i=0; i<=26; i++)
28         for (int j=0; j<=i; j++)
29             if (!j||i==j)
30                 com[i][j]=1;
31             else
32                 com[i][j]=com[i-1][j-1]+com[i-1][j];
33     com[0][0]=0;
34 }
35 int main()
36 {
37     init();
38     char num[20];
39     scanf("%s",num);
40     int len=strlen(num),k;
41     for (k=1;k<=len-1;k++)  /*判断是否为递增字符串*/
42         if (num[k]<=num[k-1]) break;
43     if (k!=len)
44     {
45         cout<<0<<endl;
46         return 0;
47     }
48     long long ans=0;
49     /*任意长度小于等于len的数都在其前面*/
50     for (int i=1; i<=len-1; i++)
51         ans+=com[26][i];
52     /*计算最高位为[a,num[0])的个数*/
53     for (char i=‘a‘; i<num[0]; i++)
54         ans+=com[‘z‘-i][len-1];
55     /*计算前j位相同的个数*/
56     for (int j=1;j<=len-1;j++)
57         /*第j位可能的数字为[num[j-1]+1,num[j]) */
58         for (char i=num[j-1]+1;i<num[j];i++)
59             ans+=com[‘z‘-i][len-1-j];
60     cout<<ans+1<<endl;
61     return 0;
62 }
时间: 2024-08-06 11:58:08

POJ1850——Code(组合数学)的相关文章

poj1496 Word Index / poj1850 Code(组合数学)

poj1850 Code 题意:输出若干个给定的字符串($length<=10$)在字典序中的位置,字符串中的字母必须严格递增. 读取到非法字符串时,输出“0”,终止程序.(poj1496:继续读取) 我们分成2种情况讨论字典序小于给定字符串的字符串个数 1.长度比给定字符串小 其实长度为$i$的字符串的个数就是$C(26,i)$ 因为我们随机选取$i$个字符后,从小到大依次取出,是可以保证字符串是有序的. 2.长度等于给定字符串 我们枚举每一位,计算从该位开始小于给定字符串的个数,同样可以用组

POJ1850 Code 【排列组合】

还不算是难题(嘿嘿,因为我做出来了) 很简单,找到相应的方程式解就是了(中间也犯了一个很2的错误,把9+10+11+...+99写成(1+90)*90/2,改过来后就好了) 不多说,code is below #include <iostream> #include <cstdio> #include <cstring> using namespace std; int combi(int a,int b) { int sum=1; int j=1; for(int i

poj1850 Code【组合数学】By cellur925

题意: * 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序字符串)* a - 1* b - 2* ...* z - 26* ab - 27* ...* az - 51* bc - 52* ...* vwxyz - 83681* 输入字符串由小写字母 a-z 组成字符串为升序,根据字符串输出在字典里的序列号为多少. 很容易地我们可以想到,设输入的字符串长度为len,我们可以用组合数求出所有小于len长度的字符串数量,(感觉这一步很好理解,反而许多题解给出了详细的证明?). 然

POJ 1850 Code 组合数学

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

poj1850(Code)

题目地址:Code 题目大意: 按照字典序的顺序从小写字母a开始按顺序给出序列 a - 1     b - 2     ...     z - 26     ab - 27     ...     az - 51     bc - 52     ...     vwxyz - 83681     ... 输入字符串由小写字母a-z组成字符串为升序,根据字符串输出在字典里的序列号为多少. 解题思路: 排列组合,记住公式: 先看字符的长度一个字符的时候是26为C(26,1),两个字符的时候分别从以a

poj:1850 Code(组合数学?数位dp!)

题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][pre]前pos位,前一位是pre有几个满足条件的字符串,其实等同于这个字符串的序号是多少 好像数位dp的博客真没什么东西好写的... #include<iostream> #include<cstring> #include<cstdlib> #include<cs

POJ1850 Code【全排列】

题目链接: http://poj.org/problem?id=1850 题目大意: 给你一个字符串str,输出str在字典序全排列中的位置(从小到大排列). 解题思路: 参考博文:http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122760.html 先判断str是不是升序序列,如果是升序序列,则为字典序的第1个,输出1. 不符合第一步的话,则分为两步计算str的字典序位置. 先计算比 str 的长度少的字符串总个数. 再计算长度

POJ1850 Code(组合+康托展开)

题目问一个合法字符串的字典序是第几个,合法的字符串是指里面的字符严格递增. 先判断合不合法,然后用类似康托展开的过程去求.大概过程就是用组合数算出某长度某前缀有几个,累加起来. 真难一遍写对.. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 bool islegal(char *s){ 5 for(int i=1; s[i]; ++i){ 6 if(s[i]<=s[i-1]) return 0;

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY