Submission

Status:

PPPPPPPPPPPPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Kei

Problemset: สุ่มสลับ

Language: cpp

Time: 0.005 second

Submitted On: 2026-10-08 16:16:40

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;



void solve(){
    int n; string s;
    cin >> n >> s;

    long long fact[13];
    fact[0] = 1;                              // (1) what is 0! ?
    for(int i=1;i<=12;i++) fact[i] = i*fact[i-1];       // (2)

    bool used[12] = {false};
    long long rank = 1;                       // (3) the answer is 1-indexed

    for(int i=0;i<n;i++){
        int c = s[i]-'A';
        int smaller = 0;
        for(int x=0; x<c; x++)                // (4) which letters are smaller than c?
            if(!used[x]) smaller++;                  // (5) ...but only count them if...
        rank += smaller * fact[n-i-1];            // (6) how many positions are AFTER position i?
        used[c] = true;                          // (7)
    }
    cout << rank;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}