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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
using ll = long long;

using namespace std;

int main() {
    ll N;
    string S;
    cin >> N >> S;
    ll sum[N][10];//sum[i][j]i + 1文字目までのjの個数
    for(ll i = 0; i < N; i++){
        for(ll j = 0; j < 10; j++){
            sum[i][j] = 0;
        }
    }
    for(ll i = 0; i < N; i++){
        //一つ前をトレスした上で追加
        if(i != 0){
            for(int j = 0; j < 10; j++){
                sum[i][j] = sum[i - 1][j];
            }
        }
        sum[i][S[i] - '0']++;
        for(int j = 0; j < 10; j++){
            //cout << sum[i][j] << " ";
        }
        //cout << endl;
    }
    bool isEnable[10][10][10];
    for(int xi = 0; xi < 10; xi++){
        for(int yi = 0; yi < 10; yi++){
            for(int zi = 0; zi < 10; zi++){
                isEnable[xi][yi][zi] = false;
            }
        }
    }

    for(ll i = 1; i < N - 1; i++){
        ll sumB [10];
        ll sumA [10];
        int current = S[i] - '0';
        for(int tmpi = 0; tmpi < 10; tmpi++){
            sumB[tmpi] = sum[i - 1][tmpi];
            sumA[tmpi] = sum[N - 1][tmpi] - sum[i][tmpi];
        }
        for(int bi = 0; bi < 10; bi++){
            for(int ai = 0; ai < 10; ai++){
                if(sumA[ai] > 0 && sumB[bi] > 0){
                    isEnable[bi][current][ai] = true;
                }
            }
        }
    }
    ll result = 0;
    for(int xi = 0; xi < 10; xi++){
        for(int yi = 0; yi < 10; yi++){
            for(int zi = 0; zi < 10; zi++){
                if(isEnable[xi][yi][zi]){
                    //cout << xi << yi << zi << endl;
                    result++;
                }
            }
        }
    }
    cout << result << endl;

}