POJ 1850/1496 combined mathematics
Question address:
POJ 1496 Word Index
POJ 1850 Code
Question:
1. Each word is auto-incrementing.
2. Words of the same length are rehearsed in Lexicographic Order.
3. We sort the word order
Returns the sequence number of a word.
Analysis:
Combined mathematics...
Introduce the formula and then use the Yang Hui triangle to list the number of groups. For details, see http://blog.csdn.net/lyy289065406/article/details/6648.pdf.
I am still too weak. Orz...
Code:
1496:
/** Author: illuz
* File: 1496.cpp* Create Date: 2014-05-24 23:38:54* Descripton: */#include
#include
#include
const int N = 27;int c[N][N], len, sum;char str[11];void init() {for (int i = 0; i < N; i++)for (int j = 0; j <= i; j++)if (!j || i == j)c[i][j] = 1;elsec[i][j] = c[i - 1][j - 1] + c[i - 1][j];c[0][0] = 0;}int main() {init();while (~scanf("%s", str)) {len = strlen(str);bool flag = true;for (int i = 1; i < len; i++)if (str[i - 1] >= str[i]) {puts("0");flag = false;}if (!flag)continue;sum = 1;for (int i = 1; i < len; i++) {sum += c[26][i];}for (int i = 0; i < len; i++) {char ch = (0 == i) ? 'a' : str[i - 1] + 1;while (ch <= str[i] - 1) {sum += c['z' - ch][len - i - 1];ch++;}}printf("%d\n", sum);}return 0;}
1850:
/** Author: illuz
* File: 1850.cpp* Create Date: 2014-05-24 23:10:11* Descripton: */#include
#include
#include
const int N = 27;int c[N][N], len, sum;char str[11];void init() {for (int i = 0; i < N; i++)for (int j = 0; j <= i; j++)if (!j || i == j)c[i][j] = 1;elsec[i][j] = c[i - 1][j - 1] + c[i - 1][j];c[0][0] = 0;}int main() {init();scanf("%s", str);len = strlen(str);for (int i = 1; i < len; i++)if (str[i - 1] >= str[i]) {puts("0");return 0;}sum = 1;for (int i = 1; i < len; i++) {sum += c[26][i];}for (int i = 0; i < len; i++) {char ch = (0 == i) ? 'a' : str[i - 1] + 1;while (ch <= str[i] - 1) {sum += c['z' - ch][len - i - 1];ch++;}}printf("%d\n", sum);return 0;}