pku1496 Word Index

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

求组合数

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int C(int x, int y)
 5 {
 6     return y? C(x-1, y-1)*x/y: 1;
 7 }
 8 
 9 int main()
10 {
11     char s[12] = "\0";
12     int a[12] = {0};
13     int i, j, sum, n, flag;
14     while(~scanf("%s", s+1))
15     {
16         flag = 0;
17         for(i=1; s[i]; i++)
18         {
19             a[i] = s[i]-'a'+1;
20             if(a[i] <= a[i-1])
21             {
22                 printf("0\n");
23                 flag = 1;
24                 break;
25             }
26         }
27         if(flag)
28         {
29             continue;
30         }
31         n = i-1;
32         sum = 1;
33         for(i=1; i<n; i++)
34         {
35             sum += C(26, i);
36         }
37         for(i=1; i<=n; i++)
38         {
39             for(j=a[i-1]+1; j<a[i]; j++)
40             {
41                 sum += C(26-j, n-i);
42             }
43         }
44         printf("%d\n", sum);
45         memset(s, 0, sizeof(s));
46     }
47     return 0;
48 }

 

posted @ 2013-01-21 00:19  Yuan1991  阅读(109)  评论(0编辑  收藏  举报