您的位置:首页 > 其它

POJ 1850Code 组合数运用

2016-02-20 11:07 183 查看
CODE

题意 给一个串让你写出对应的编码数

该串必须是 编码先按长度进行编码 对于每个串来说他们的每个字符是依次增大的(不满足此条件无法编码输出0)

思路:

1 先把 26 的组合情况打表出来 长度的编码数量

2 把比该串短的编码数求出来

3 把相同长度的但字母在其前面的编码数求出来

组合数的计算公式

C(n,m)=C(n-1,m-1)+c(n-1,m);

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
int a[28][28];
int solve()
{
memset(a,0,sizeof(a));
for(int i=0; i<=26; i++)    //打表组合数统计不同长度编码可以代表的个数
{
for(int j=0; j<=i; j++)
{
if(!j||i==j)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
}
int  main()
{
solve();
char s[1000];
scanf("%s",s);
int len=strlen(s);
for(int i=1; i<len; i++)   //判断是否满足升序
{
if(s[i]<=s[i-1])
{
printf("0\n");
return 0;
}
}

long long sum=1;
for(int i=1; i<len; i++)  ///统计比该字符串短的编码数量
{
sum=sum+(long long)a[26][i];
}
char ch;
for(int i=0; i<len; i++)  ///统计相同长度字符串但位于前面的编码的个数
{                                ///对每个位置进行枚举
if(i==0)
ch='a';
else
ch=s[i-1]+1;   //升续规则
while(ch<s[i])
{
sum=sum+(long long)a['z'-ch][len-i-1];
///'z'-ch : 小于等于ch的字符不允许再被选择,所以当前能够选择的字符总数为'z'-ch
///len-1-i  : ch位置后面(不包括ch)剩下的位数,就是从'z'-ch选择len-1-i个字符
ch++;
}
}
printf("%lld\n",sum);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: