标签:
题目链接:http://acm.swust.edu.cn/problem/715/
1 2 … 26 27 28 …
a b … z ab ac …
1
2
3
|
2
a
b
|
1
2
|
1
2
|
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 char s[7]; 6 int cur[27][27] = { 1 }; 7 8 //预处理 利用杨辉三角计算组合数 9 void init(){ 10 int i, left, right; 11 for (i = 1; i <= 26; i++){ 12 cur[i][0] = cur[i][i] = 1; 13 left = 1, right = i - 1; 14 while (left <= right){ 15 cur[i][left] = cur[i - 1][left - 1] + cur[i - 1][left]; 16 cur[i][right--] = cur[i][left++];//组合数性质cur[i][j]=cur[i][i-j]; 17 } 18 } 19 } 20 21 //长度小于len的串的个数 22 int minlen_num(int len){ 23 int i, cnt = 0; 24 for (i = 1; i < len; i++) 25 cnt += cur[26][i]; 26 return cnt; 27 } 28 29 //当前长度下当前串前面的个数 30 int enquallen_num(int len){ 31 int i, j, cnt = 0, pre = -1, tmp; 32 for (i = 0; i < len; i++){ 33 tmp = s[i] - ‘a‘; 34 for (j = pre + 1; j < tmp; j++) 35 cnt += cur[26 - j - 1][len - i - 1]; 36 pre = tmp; 37 } 38 return cnt; 39 } 40 41 bool judge(int len){ 42 for (int i = 1; i < len; i++) 43 if (s[i] <= s[i - 1]) 44 return false; 45 return true; 46 } 47 48 int main(){ 49 init(); 50 int t, cnt, len; 51 cin >> t; 52 while (t--){ 53 cnt = 0; 54 cin >> s; 55 len = strlen(s); 56 if (!judge(len)) cout << 0 << endl; 57 else{ 58 cnt += minlen_num(len); 59 cnt += enquallen_num(len); 60 cout << cnt + 1 << endl; 61 } 62 } 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/zYx-ac/p/4564357.html