POJ 1850 Code

解题思路:组合数学题,注意题目要求升序排列

类似于POJ3252,c[i][j]存储(choose i from j 的)组合值

  
    
1 #include < iostream >
2   using namespace std;
3 int main()
4 {
5 int ans,l,i,j,p,q,c[ 27 ][ 27 ] = { 0 },nc[ 11 ],IsOk = 1 ;
6 char ch[ 11 ];
7 for (i = 0 ;i < 27 ;i ++ )
8 for (c[i][i] = 1 ,j = i + 1 ;j < 27 ;j ++ )
9 c[i][j] = c[i][j - 1 ] * j / (j - i);
10 scanf( " %s " , ch);l = strlen(ch);
11 for (ans = i = 0 ;i < l && IsOk;i ++ ){nc[i + 1 ] = ch[i] - ' a ' + 1 ; if (ch[i] > ' z ' || ch[i] < ' a ' || nc[i + 1 ] <= nc[i])IsOk = 0 ;}
12 if (IsOk)
13 {
14 for (i = 1 ;i < l;i ++ )
15 ans += c[i][ 26 ];
16 for (p = 0 ,i = 1 ,q = nc[i];i <= l;i ++ ,p = nc[i - 1 ],q = nc[i])
17 for (j = 27 - q;j < 26 - p;j ++ )ans += c[l - i][j];
18 ans ++ ;
19 }
20 printf( " %d\n " ,ans);
21 return 0 ;
22 }

 

你可能感兴趣的:(code)