Code (组合数)

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7184   Accepted: 3353

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

忽略了输出0的情况,wa了若干次。。。
 1 #include<stdio.h>
 2 #include<string.h>
 3 int c[30][30];
 4 
 5 void init()
 6 {
 7     memset(c,0,sizeof(c));
 8     for(int i = 0; i <= 26; i++)
 9     {
10         c[i][0] = 1;
11         c[i][i] = 1;
12     }
13 
14     for(int i = 2; i <= 26; i++)
15     {
16         for(int j = 1; j < i; j++)
17         {
18             c[i][j] = c[i-1][j-1] + c[i-1][j];
19         }
20     }
21 }
22 
23 
24 int main()
25 {
26     init();
27     int i,j,sum;
28     char s[12];
29     scanf("%s",s);
30     int len = strlen(s);
31 
32     int flag = 1;
33     for(i = 0; i < len; i++)
34     {
35         for(j = i+1; j < len; j++)
36         {
37             if(s[i] >= s[j])
38             {
39                flag = 0;
40                 break;
41             }
42         }
43         if(flag == 0)
44             break;
45     }
46     if(flag == 0)
47         printf("0\n");
48     else
49     {
50         sum = 0;
51         for(i = 1; i <= len-1; i++)
52             sum += c[26][i];
53 
54         for(j = 0; j <= s[0]-'a'-1; j++)
55             sum += c[25-j][len-1];
56 
57         for(i = 1; i < len; i++)
58         {
59             for(j = s[i-1]-'a'+1; j <= s[i]-'a'-1; j++)
60             {
61                 sum += c[25-j][len-1-i];
62             }
63         }
64 
65         printf("%d\n",sum+1);
66     }
67 
68     return 0;
69 }
View Code

 

posted on 2013-09-17 20:42  straw_berry  阅读(254)  评论(0编辑  收藏  举报