POJ 1850 Code

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 // TEST

const 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值