NYOJ139 the number (string decoding)
Now there are 12 characters in abcdefghijkl. Sort all of them in Lexicographic Order and give any sort. Which of the following is the smallest of all the arrays?
-
The first line of the input has an integer n (0 Then there are n rows, each row is an arrangement; the output is an integer m, occupying a row, m indicates the number of places in the arrangement; example Input
3abcdefghijklhgebkflacdjigfkedhjblcia
Sample output
1302715242260726926
Question Analysis:
Do you have any questions about this question? I have answered them in the algorithm getting started classic,For each character s, if k characters are less than s, the sorting of the current sequence will increase k * f [12-i].
AC code:
/*** Returns a string that consists of only ~ L composition, each occurrence, returns the lexicographic size of the string * for each character s, if k characters are less than s after it, then the sorting of the current sequence will increase k * f [12-i] * Where f is the factorial of each number and I is the position of s. Therefore, you only need to simulate each bit for comparison. */# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; int main () {long f [15]; f [1] = 1; for (int I = 2; I <= 12; I ++) {f [I] = f [I-1] * I;} char s [15]; int t; scanf (% d, & t); while (t --) {scanf (% s, s + 1); long sum = 0; for (int I = 1; I <= 12; I ++) {int k = 0; for (int j = I + 1; j <= 12; j ++) {if (s [I]> s [j]) k ++ ;} sum + = f [12-i] * k;} printf (% lld, sum + 1);} return 0 ;}