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: Runtime Error)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<bitset>
#include<queue>
#define ll long long
#define itn int
#define co(ans) cout<<ans<<endl;
#define COYE cout<<"YES"<<endl;
#define COYe cout<<"Yes"<<endl;
#define COye cout<<"yes"<<endl;
#define CONO cout<<"NO"<<endl;
#define CONo cout<<"No"<<endl;
#define COno cout<<"no"<<endl;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define INF ((1LL<<62)-(1LL<<31))
#define MOD 1000000007
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using namespace std;


int main(){
  ll N,ans=0;
  string S;
  cin>>N>>S;
  
  bool AA[N][10],BB[N][10];
  REP(i,N){
    REP(j,10){
      AA[i][j]=false;
      BB[i][j]=false;
    }
  }
  
  bool CAN[1000];
  REP(i,1000) CAN[i]=false;
  
  
  for(int i=0; i<S.size(); ++i){
    if(i<S.size()-1){
      if(AA[i+1][S.at(i)]==false){
        AA[i+1][S.at(i)]=true;
      }
    }
    if(i>0){
      for(int j=0; j<10; ++j){
        if(AA[i-1][j]==true) AA[i][j]=true;
      }
    }
  }
  
  for(int i=S.size()-1; i>=0; --i){
    if(i>0){
      if(BB[i-1][S.at(i)]==false){
        BB[i-1][S.at(i)]=true;
      }
    }
    if(i<S.size()-1){
      for(int j=0; j<10; ++j){
        if(BB[i+1][j]==true) BB[i][j]=true;
      }
    }
  }
  
  for(int i=1; i<=S.size()-2; ++i){
    for(int j=0; j<10; ++j){
      for(int k=0; k<10; ++k){
        if(AA[i][j]==true && BB[i][k]==true) CAN[j*100+S.at(i)*10+k]=true;
      }
    }
  }
  
  for(int i=0; i<1000; ++i){
    if(CAN[i]) ans++;
  }
  
  co(ans);
}