poj1496(组合数学)-与1850几乎一样

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

Word Index
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 3969Accepted: 2247

Description

Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.

Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,

abc aep gwz

are all valid three-letter words, whereas

aab are cat

are not.

For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:

a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681

Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long. For each word read, if the word is invalid give the number 0. If the word read is valid, give the word's position index in the above alphabetical list.

Input

The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line.

The input will be terminated by end-of-file.

Output

The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.

Sample Input

z
a
cat
vwxyz

Sample Output

26
1
0
83681

Source

详解参照上一篇文章poj1850。。
#include
#include
#include
using namespace std;
int c[27][27]={0};
void Init()
{
      for(int i=0;i<=26;i++)
      {
              for(int j=0;j<=i;j++)
              {
                      if(j==0||i==j)
                      {
                              c[i][j]=1;
                      }
                      else c[i][j]=c[i-1][j-1]+c[i-1][j];
              }
      }
      c[0][0]=0;
      return;
}
int main()
{
      char str[12];
      while(scanf("%s",str)!=EOF)
      {
              bool flag=false;
              Init();
              int len=strlen(str);
              for(int i=0;i<=len-2;i++)
              {
                      if(str[i]>=str[i+1])
                      {
                              printf("0\n");
                              flag=true;
                              //return 0;
                      }
              }
              int sum=0;
              if(flag==false)
              {
                      for(int i=1;i
                      {
                              sum+=c[26][i];
                              //printf()
                      }
                      for(int i=0;i
                      {
                              char ch=(i==0)?'a':str[i-1]+1;
                              while(ch<=str[i]-1)
                              {
                                      sum+=c['z'-ch][len-i-1];
                                      ch++;
                              }
                      }
                      printf("%d\n",++sum);
              }
      }
      return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值