poj 1850 (组合)

code

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9282 Accepted: 4432

Description

Transmitting (传输) and  memorizing (记忆) information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is  associated (交往) to a character  sequence (序列). It is considered that the words are made only of small characters of the English  alphabet (字母表) a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).

The coding system works like this: 
• The words are arranged in the increasing order of their length.
• The words with the same length are arranged in  lexicographical (辞典编纂的) order (the order from the dictionary). 
• We  codify (编纂) these words by their numbering, starting with a, as follows:
a - 1 
b - 2
...
z - 26
ab - 27
...
az - 51
bc - 52
...
vwxyz - 83681
...

Specify (指定) for a given word if it can be codified according to this coding system. For the  affirmative (肯定的) case specify its code. 

Input

The only line contains a word. There are some  constraints (约束):
• The word is maximum 10 letters length
• The English  alphabet (字母表) has 26 characters.

Output

The  output (输出) will contain the code of the given word, or 0 if the word can not be codified.

Sample Input

bf

Sample Output

55

Source


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;

char a[15];
 int c[30][15];
//long long int c(int n,int m)
//{
//    if(m>n-m)m=n-m;
//    int i=n,j=m;
//    double s=1;
//    for( ; j>0; j--,i--)
//    {
//        s=s*(double)i/(double)j;
//    }
//    return (long long int )s;
//}
//long long int b[15];
int main()
{
    c[0][0]=0;
    for(int i=1; i<=26; i++)         
        for(int j=0; j<=i; j++)
        {
            if(!j||i==j)c[i][j]=1;
            else c[i][j]=c[i-1][j-1]+c[i-1][j];<span style="font-family: Arial, Helvetica, sans-serif;">    //</span><span style="color:#ff0000;font-family: Arial, Helvetica, sans-serif;">初始化</span>

        }
	 int sum;
	 while(~scanf("%s",a))
      //scanf("%s",a);
    {
        int l=strlen(a);
        int t=1;
        for(int i=1; i<l; i++)
        {
            if(a[i]<=a[i-1])
            {
                t=0;
                break;
            }
        }
        if(!t)printf("0\n");
        else
        {
            sum=0;
            for(int i=1; i<l; i++)
                sum+=c[26][i];
            // printf("%d\n",sum);
            for(int i=0; i<l; i++)
            {
                int s=a[i]-'a';
                if(i==0)                         //找出在此字母之前的组合
                {
                     if(s!=0)
                    {
                        for(int j=1; j<=s; j++)
                        {
                            sum+=c[26-j][l-1];
                        }
                    }

                }
                //else if(i==l-1)sum+=(s-(a[i-1]-'a'+1));
                else
                {
                    int ss=a[i-1]-'a';
                    for(int j=ss+1; j<s; j++)
                    {
                        sum+=c[26-j-1][l-i-1];
                    }
                    // sum++;
                    // printf("***sum=%d\n",sum);
                }
                // printf("%d\n",sum);
            }
            sum++;
            printf("%d\n",sum);
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值