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;}
0 0
原创粉丝点击