Topic Links:http://poj.org/problem?id=1850
Main topic:give you a string str, the position of the output str in the whole dictionary order (from small to large).
Problem Solving Ideas:reference post: http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122760.htmlfirst determine if STR is not ascending sequence, if it is ascending sequence, then 1th of the dictionary order, output 1. If the first step is not met, it is divided into two steps to calculate the dictionary order position of Str. The total number of strings that are less than the length of STR is calculated first. The number of strings compared to the STR dictionary sequence is computed as the length of the second. let's start with the first step:the number of strings with a length of 1 is C (26,1). the number of strings with a length of 2 is C (26,2). ...the number of strings with a length of 2 is C (26,2). Take Another look at the second step:for each bit of STR str[i] and the left adjacent str[i-1], if str[i] > str[i-1], the current positionStr[i] At leastlarger than the str[i-1 of the previous one). The answer ans is to add a letter larger than str[i] to form the number of strings of len-1-i length, that is, c[26-ch][len-1-i]. ch from str[i] to str[i-1] + 1.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int c[27][27];void Zuhe () {c[0][0] = c[1][0] = c[1][1] = 1; for (int i = 2; I <=; ++i) {c[i][0] = c[i][i] = 1; for (int j = 1; j < i; ++j) c[i][j] = C[i-1][j] + c[i-1][j-1]; }}char Str[20];int Main () {Zuhe (); while (~SCANF ("%s", str)) {int ans = 0; int len = strlen (str); bool Falg = true; for (int i = 1; i < Len; ++i) {if (Str[i] <= str[i-1]) {falg = false; Break }} if (!falg) {printf ("0\n"); Continue } int time = len-1; while (time) {ans + = c[26][time]; time--; } int ch,ch1; for (int i = 0; i < len; ++i) {ch = str[i]-' a '; if (i = = 0) ch1 = 0; ELSE CH1 = Str[i-1]-' a ' + 1; while (Ch > ch1) {ans + = c[26-ch][len-1-i]; ch--; }} ans++; printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ1850 Code "full array"