Problem:
Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

4 \leq N \leq 30000

S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.

Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

Erase the first digit of S and set 224.

Erase the second digit of S and set 024.

Erase the third digit of S and set 024.

Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.

Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<int, int>  pii;
typedef pair<ld, ld>  pld;
struct node{
    set<int> one;
    set<pii> two;
    set<pair<int, pii>> three;
};
int n;
int a[30009];
node f(int l, int r){
    node ret;
    if(l == r){
        ret.one.insert(a[l]);
        return ret;
    }
    node c1 = f(l, (l+r)/2);
    node c2 = f((l+r)/2+1, r);
    for(auto u : c1.one)
        ret.one.insert(u);
    for(auto u : c2.one)
        ret.one.insert(u);
    
    for(auto u : c1.two)
        ret.two.insert(u);
    for(auto u : c2.two)
        ret.two.insert(u);
    
    for(auto u : c1.three)
        ret.three.insert(u);
    for(auto u : c2.three)
        ret.three.insert(u);
    
    for(auto u1 : c1.one)
        for(auto u2 : c2.one)
            ret.two.insert({u1, u2});
    
    for(auto u1 : c1.one)
        for(auto u2 : c2.two)
            ret.three.insert({u1, u2});
    
    for(auto u1 : c1.two)
        for(auto u2 : c2.one)
            ret.three.insert({u1.ff, {u1.ss, u2}});

    return ret;
}
int input(){
    char c;
    cin >> c;
    return (c-'a');
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n;
    for(int i = 0; i < n; ++i)
        a[i] = input();
    cout << f(0, n-1).three.size() << "\n";
}