码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1850——Code(组合数学)

时间:2014-10-29 21:15:45      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   ar   for   sp   

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: 369372123@qq.com
 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 }

 

POJ1850——Code(组合数学)

标签:des   style   blog   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/Enumz/p/4060391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!