Code POJ - 1850 组合数学

 

 

 

  题意 :字符串从a=1 b=2 c=3....z=26  ab=27开始编号 每个都是升序的 给出字符串问是几号

  思路:主要是要看n位字符串有多少个 这里需要用组合数学的思想  组合数用杨辉三角形递推出

  参考:https://blog.csdn.net/lyy289065406/article/details/6648492

 

 

 

然后就没有什么难度了

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=3e5+5;
 5 char s[maxn];
 6 int c[50][50];
 7 void init(){
 8     c[0][0]=0;
 9     for(int i=0;i<=26;i++){
10         for(int j=0;j<=i;j++){
11             if(!j||i==j){
12                 c[i][j]=1;
13             }
14             else {
15                 c[i][j]=c[i-1][j-1]+c[i-1][j];
16             }
17         }
18     }
19 }
20 int main(){
21     int n;
22     init();
23     char s[20];
24     while(scanf("%s",s)==1){
25         int len=strlen(s);
26         bool flag=0;
27         for(int i=0;i<len-1;i++){
28             if(s[i]>=s[i+1]){
29                 printf("0\n");
30                 flag=1;
31                 break;
32             }
33         }
34         if(!flag){
35             int sum=0;
36             for(int i=1;i<len;i++){
37                 sum+=c[26][i];
38             }
39             for(int i=0;i<len;i++){
40                 char ch=(!i)?'a':s[i-1]+1;
41                 while(ch<=s[i]-1){
42                     sum+=c['z'-ch][len-1-i];//选了c后剩下的位置能选多少个 用组合数
43                     ch++;
44                 }
45             }
46             printf("%d\n",sum+1);
47         }
48 
49     }
50 
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/ttttttttrx/p/10264951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值