POJ 1850 Code

来源:互联网 发布:英文翻译软件有哪些 编辑:程序博客网 时间:2024/06/16 18:05

POJ 1850 Code

题意:
给你一个序列,如果不是严格升序的序列就输出0,否则判断这个序列是第几位。
思路:
分成两部分:
第一部分:先算长度小于次串的所有序列有多少个。其实就是一个排列组合,也就是C(26,n),n是串的长度。就是从26个字母里面选n个字母的组合。
第二部分:
c[0], c[1], c[2],…,c[n];
我们发现如果c[0]是a的话,那么后面就剩下25种选择的方法了,如果c[0]是d的话,那么后面就剩下23种的方法了,而且这个规律对后面的c[1],c[2],…,c[n]都有效。也就是说,除掉这个字母,后面的就是C(n, k)种。其中n是还剩几种选择的字母,k是除掉这个字母之后右边的字母的个数。
最后别忘了加上自己本身的1。
Code:

#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<bitset>#include<queue>#include<stack>#include<list>#include<map>#include<set>#define TEST#define LL long long#define Mt(f, x) memset(f, x, sizeof(f));#define rep(i, s, e) for(int i = (s); i <= (e); ++i)#ifdef TEST    #define See(a) cout << #a << " = " << a << endl;    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;    #define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';} cout << endl;    #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee)}#else    #define See(a)    #define See2(a, b)    #define debug(a, s, e)    #define debug2(a, s, e, ss, ee)#endif // TESTconst int MAX = 2e9;const int MIN = -2e9;const double eps = 1e-8;const double PI = acos(-1.0);using namespace std;int c[30][30], f[15];int getC(int n, int k)//利用递推求组合数{    if(k == 0) return 1;    if(c[n][k] != -1) return c[n][k];    return c[n][k] = getC(n, k - 1) * ((LL)n - k + 1) / k;}void init()//先把组合数的前缀和求出来{    for(int i = 1; i <= 11; ++i)    {        f[i] = f[i - 1] + getC(26, i);    }}int main(){    Mt(c, -1);    init();    char ch[15];    while(~scanf("%s", ch))    {        bool flag = true;        int len = strlen(ch);        for(int i = 1; i < len; ++i)        {            if(ch[i] <= ch[i - 1])            {                flag = false;                break;            }        }        if(!flag)        {            printf("0\n");            continue;        }        int ans = 0;        ans += f[len - 1];        char pre = 'a' - 1;        for(int i = 0; i < len; ++i)        {            int t = ch[i] - pre - 1;            for(int k = 0; k < t; ++k)            {                ans += getC(26 - (pre - 'a' + k + 2), len - i - 1);            }            pre = ch[i];        }        printf("%d\n", ans + 1);    }    return 0;}
0 0
原创粉丝点击