#include <bits/stdc++.h> using namespace std; int counter [256]; int factorial(int n) { int f = 1; while(n>0) f *= n,n--; return f; } void make_array(string s) { for(int i = 0; i < s[i]; i++) counter[s[i]]++; for(int i = 1; i < 256; i++) counter[i] += counter[i-1]; } void update_counter(char ch) { for(int i = ch; i < 256; i++) counter[i]--; } int main() { string s; cin >> s; int len = s.size(); int rank = 1; int total = factorial(len); make_array(s); //contains count of characters which are present // in s and are smaller than i for(int i = 0; i < len; i++) { total /= len - i; //count number of chars smaller than s[i] //fron s[i+1] to s[len-1] rank += counter[s[i]-1]*total; //reduce count of characters greater than s[i] update_counter(s[i]); } cout << "Rank is : " << rank << endl; return 0; }