zoukankan      html  css  js  c++  java
  • Code(组合数学)

    http://poj.org/problem?id=1850

    题意:求所给字符串按照题目的编码规则,它的编码应是多少?

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define  LL long long
     4 
     5 double C(int a,int b)//计算组合数C(a,b)
     6 {
     7     if(a < b)
     8         return 0;
     9     double res = 1.0;
    10     while(b > 0)
    11     {
    12         res*=(double)a--/(double)b--;
    13     }
    14     return res;
    15 }
    16 int main()
    17 {
    18     char s[120];
    19     while(~scanf("%s",s))
    20     {
    21         LL ans = 0;
    22         int len = strlen(s);
    23         for (int i = 1; i < len; i++)
    24         {
    25             if (s[i]<s[i-1])//不合法的情况
    26             {
    27                 printf("0
    ");
    28                 return 0;
    29             }
    30         }
    31         for (int i = 1; i < len; i++)//比所求字符串短的合法串的数目
    32         {
    33             ans+=C(26,i);
    34         }
    35         //和所求字符串长度相等的合法串的数目
    36         for (int i = 'a'; i < s[0]; i++)//单独比较第一个字母
    37         {
    38             ans+=C('z'-i,len-1);
    39         }
    40         for (int i = 1; i < len; i++)
    41         {
    42             for (int j = s[i-1]+1; j < s[i]; j++)
    43             {
    44                 ans+=C('z'-j,len-i-1);
    45             }
    46         }
    47         printf("%lld
    ",ans+1);//所求字符串前面的合法串的数目加上串本身
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    日期格式化
    堆栈
    编写自己的C头文件
    线性表(gcc实现)
    排序的稳定性
    git创建和合并分支
    当单选input框改变时触发
    css样式定义
    div块显示在一行
    redis数据结构(一)
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3545030.html
Copyright © 2011-2022 走看看