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



Sample input:
4
0224


Sample output:
3

Submission status: Accepted

Solution code:
import sys
N = int(input())
S = list(input())
xList = []
yList = []
zList = []
sum = 0
a = 0
while a < N-2:
    if xList.count(S[a]) == 0:
        xList.append(S[a])
        b = a+1
        a += 1
        while b < N-1:
            if yList.count(S[b]) == 0:
                yList.append(S[b])
                c = b+1
                b += 1
                while c < N:
                    if zList.count(S[c]) == 0:
                        zList.append(S[c])
                        c += 1
                        sum += 1
                    else:
                        c += 1
                print(zList,file=sys.stderr)
                zList = []
            else:
                b += 1
        yList = []
    else:
        a += 1
print(sum)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=0
for i in range(1000):
  l=(3-len(str(i)))*'0'+str(i)
  #print("l: ",l)
  #break
  if l[0] in s:
    #print("l[0]: ",l[0])
    #break
    p1=s.index(l[0])
    #print("p1: ",p1)
    #break
    if l[1] in s[p1+1:]:
      #print("l[1]: ",l[1])
      #break
      p2=p1+s[p1+1:].index(l[1])+1
      #print("p2: ",p2)
      #break
      if l[2] in s[p2+1:]:
        #print("l[2]: ",l[2])
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=0
for i in range(1000):
  l=(3-len(str(i)))*'0'+str(i)
  if l[0] in s:
    p1=s.index(l[0])
    if l[1] in s[p1+1:]:
      p2=p1+s[p1+1:].index(l[1])+1
      if l[2] in s[p2+1:]:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
l = int(input())
s = input()
r = set()
cx = set(s)
for x in range(0, l - 2):
    if not cx:
        break
    if s[x] not in cx:
        continue
    cx.remove(s[x])
    cy = set(s[x+1:])
    for y in range(x + 1, l - 1):
        if not cy:
            break
        if s[y] not in cy:
            continue
        cy.remove(s[y])
        cz = set(s[y+1:])
        for z in range(y + 1, l):
            if not cz:
                break
            if s[z] not in cz:
                continue
            cz.remove(s[z])
            r.add(s[x] + s[y] + s[z])

print(len(r))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
c = 0
for i in range(1000):
    num_str = "{0:0>3}".format(i)
    j = 0
    for s in S:
        if num_str[j] == s:
            j += 1
        if j == 3:
            c += 1
            break
 
print(c)

Submission status: Accepted

Solution code:
N = input()
S = input()
ans = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            a = S.find(str(i))
            if a == -1:
                continue
            b = S.find(str(j), a+1)
            if b == -1:
                continue
            c = S.find(str(k), b+1)
            if c == -1:
                continue
            ans+=1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 0
# 000 - 999 まで全探索
for i in range(1000):
    t = str(i).zfill(3)

    # 1桁目が存在するか
    if t[0] in S:
        t1 = S.index(t[0])
        # 2桁目を1桁目のidx + 1から探す
        if t[1] in S[t1 + 1:]:
            # 1桁目のidx + 2桁目のidx + 1 桁目から3文字目を探す
            t2 = t1 + S[t1 + 1:].index(t[1]) + 1
            if t[2] in S[t2 + 1:]:
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
s = S()
cnt = 0
ans = set()
one_digit_set = set()
two_digit_set = set()
for i in range(n):
    for two_d in two_digit_set:
        ans.add(s[i] + two_d)
    for one_d in one_digit_set:
        two_digit_set.add(s[i] + one_d)
    one_digit_set.add(s[i])


print(len(ans))

Submission status: Accepted

Solution code:
from itertools import combinations

N = int(input())
S = input()
count = 0

ans = set({})

for password in range(0, 1000):
    s = "{:03d}".format(password)
    s1, s2, s3 = s
    
    if s1 not in S:
        continue
    i1 = S.index(s1)
    if s2 not in S[i1+1:]:
        continue
    i2 = S[i1+1:].index(s2)
    if s3 not in S[i1+1:][i2+1:]:
        continue
    i3 = S[i1+1:][i2+1:].index(s3)
    # print(s, s1, s2, s3, "|", i1, i1 + i2, i1 + i2 + i3)
    ans.add(password)

print(len(ans))

Submission status: Accepted

Solution code:
import bisect

n=int(input())
s=input()

mlist=[]
for i in range(10):
    mlist.append([])

for i in range(n):
    mlist[int(s[i])].append(i)

answer=[]

for i in range(10):
    if len(mlist[i])>0:
        left=min(mlist[i])
        for j in range(10):
            if len(mlist[j])>0:
                center=bisect.bisect_right(mlist[j], left)
                if center!=len(mlist[j]):
                    center=mlist[j][center]
                    for k in range(10):
                        right=bisect.bisect_right(mlist[k], center)
                        if right!=len(mlist[k]):
                            answer.append([i*100+j*10+k*1])
                else:
                    pass
            else:
                pass
    else:
        pass

print(len(answer))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(10):
  for j in range(10):
    for k in range(10):
      a = s.find(str(i))
      if a==-1:
        continue
      b = s.find(str(j), a+1)
      if b==-1:
        continue
      c = s.find(str(k), b+1)
      if c==-1:
        continue
      ans+=1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
S = input()

dp = list2d(N+1, 4, 0)
dp[0][0] = 1
msk = (1<<1000) - 1
for i in range(N):
    a = int(S[i])
    for j in range(4):
        dp[i+1][j] |= dp[i][j]
        if j == 0:
            dp[i+1][j+1] |= dp[i][j]<<a*100
            dp[i+1][j+1] &= msk
        if j == 1:
            dp[i+1][j+1] |= dp[i][j]<<a*10
            dp[i+1][j+1] &= msk
        if j == 2:
            dp[i+1][j+1] |= dp[i][j]<<a
            dp[i+1][j+1] &= msk
ans = 0
S = dp[N][3]
for k in range(1000):
    if S & 1<<k:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import re
 
# 入力
N = int(input())
S = str(input())
#M1, D1 = map(int, input().split())
 
si = ""
cnt = 0
for i in range(1000) :
  
  si = str(i).zfill(3)
  
  c = 0
  for x in range(N) :
    if S[x] == si[c] :
      c += 1
      if c == 3 :
        cnt += 1
        break    

print (cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

ans = 0
for i in range(1000):
	num=str(i).zfill(3)
	
	if num[0] in S:
		p1 = S.index(num[0])
		if num[1] in S[p1+1:]:
			p2 = p1+1+S[p1+1:].index(num[1])
			if num[2] in S[p2+1:]:
				ans += 1
				# print(num)


print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from collections import deque
import re
import math
import functools

def sRaw():
    return input().rstrip("\r")

def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29


def make1d_arr(n, val=INF):
    return [val for i in range(n)]


def make2d_arr(h, w, val=INF):
    return [[val for i in range(w)]for i in range(h)]


def gcd(a, b):
    if(b == 0):
        return a
    return gcd(b, a % b)


def BFord(n,es):
    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n*2):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
        if v == n-1:
            prev = v_cost[:]
    v_cost = [-INF if prev[idx]!=v_cost[idx] else v_cost[idx] for idx in range(n)]
    return v_cost
    
def ZAlgo(s):
    l = len(s)
    A = [0]*l
    i =1
    j =0
    A[0]=l
    while i<l:
        while i+j<l and s[j] == s[i+j]:
            j+=1
        if j==0:
            i+=1
            continue
        A[i]=j
        k=1
        while (i+k<l) and (k+A[k]<j):
            A[i+k] =A[k]
            k+=1
        i+=k
        j-=k
    return A


DIV = 998244353

def main():
    N = iRaw()
    S = sRaw()
    dp = [[] for i in range(N+1)]
    dp[N] = [-1]*10
    for si in range(len(S)-1,-1,-1):
        dp[si] = [dp[si+1][d] for d in range(10)]
        dp[si][int(S[si])] = si
    ans = 0 
    for i in range(1000):
        d0 = i%10
        d1 = (i//10)%10
        d2 = (i//100)%10
        idx1 = dp[0][d0]
        if idx1 == -1:
            continue
        idx2 = dp[idx1+1][d1]
        if idx2 == -1:
            continue
        if dp[idx2+1][d2] != -1:
            ans+=1
    return ans

if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

from bisect import bisect_left

N = I()
S = str(input())
d = defaultdict(list)

for i in range(N):
    d[S[i]].append(i)

ans = 0
for i in range(1000):
    now = str(i).zfill(3)
    p = 0
    flag = True
    for j in now:
        p1 = bisect_left(d[j],p)
        if p1>=len(d[j]):
            flag = False
            break
        p = d[j][p1]+1
    if flag:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

def count(poi):
    point = 0
    for i in range(poi):
        bango = str(i).zfill(3)
        count = 0
        for j in range(n):
            if s[j] == bango[count]:
                count += 1
            if count == 3:
                point += 1
                break
    return point

print(count(1000))

Submission status: Accepted

Solution code:
n = int(input())
S = input()

ans = 0
for i in range(1000):
    T = str(i).zfill(3)
    cur = 0
    for j in range(n):
        if S[j] == T[cur]:
            cur += 1
            if cur == 3:
                break
    if cur == 3:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
cnt = 0
for i in range(1000):
  c = str(i).zfill(3)
  if c[0] in S:
        index1 = S.index(c[0])
        if c[1] in S[index1+1:]:
            index2 = index1 + 1 + S[index1+1:].index(c[1])
            if c[2] in S[index2+1:]:
                cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

def search(c, text):
    pos = -1
    for i, s in enumerate(text):
        if s == c:
            pos = i
            break
    return pos

cnt = 0
for i in range(10):
    ip = search(str(i), S[0:])
    if ip < 0:
        continue
    for j in range(10):
        jp = search(str(j), S[ip+1:])
        if jp < 0:
            continue
        for k in range(10):
            kp = search(str(k), S[ip+jp+2:])
            if kp < 0:
                continue
            cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(1000):
    if i<10:
        ss="00"+str(i)
    elif i<100:
        ss="0"+str(i)
    else:
        ss=str(i)
    now=0
    for j in range(n):
        if s[j]==ss[now]:
            now+=1
            if now==3:
                ans+=1
                break
print(ans)

Submission status: Accepted

Solution code:
def solve(n, s):
    ans = 0
    for v in range(1000):
        p = 0
        for x in str(v).zfill(3):
            p = s.find(x, p)
            if p < 0:
                break
            p += 1
        ans += int(p > 0)
    return ans

assert solve(4, "0224") == 3
assert solve(6, "123123") == 17
assert solve(19, "3141592653589793238") == 329

n = int(input())
s = input()
print(solve(n, s))

Submission status: Accepted

Solution code:
def solve(n, s):
    l = {s[i]: i for i in range(n-1, -1, -1)}
    r = {s[i]: i for i in range(n)}
    S = set()
    for j in range(n):
        y = s[j]
        for x, i in l.items():
            for z, k in r.items():
                if i < j < k:
                    S.add(x+y+z)
    return len(S)

n = int(input())
s = input()
print(solve(n, s))

Submission status: Accepted

Solution code:
from __future__ import print_function
# import numpy as np
# import numpypy as np
import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd

def lcm(n,m):
    return int(n*m/gcd(n,m))

import re
import array
import copy
import functools
import operator

import collections
import itertools
import bisect
import heapq


from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace

from queue import PriorityQueue as pq

def reduce(p, q):
    common = fractions.gcd(p, q)
    return (p//common , q//common )
# from itertools import accumulate
# from collections import deque

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

import random


def main():
    n = int(input().strip())
    S = input().strip()
    cnt=0
    for SS in range(1000):
        SSs=str(SS).zfill(3)
        #eprint("SSs ",end=": ")
        #eprint(SSs)
        a=str(SSs[0])
        b=str(SSs[1])
        c=str(SSs[2])
        success_flag=0
        ii=sys.maxsize
        jj=sys.maxsize
        kk=sys.maxsize
        for i in range(len(S)):
            if S[i]==a:
                ii=min(i,ii)
            if S[i]==b and i>ii:
                jj=min(i,jj)
            if S[i]==c and i>jj:
                kk=min(i,kk)
                success_flag=1
                #eprint("ii,jj,kk ",end=": ")
                #eprint(ii,jj,kk)
            # if SSs=="224":
                #eprint("ii,jj,kk ",end=": ")
                #eprint(ii,jj,kk)
                
            
        if success_flag==1:
            cnt+=1
    print(cnt)
    return

if __name__ == '__main__':
    main()


'''
#+TITLE: math formulation

given n \in N, S \in {S}
(4 <= n == len(S) <= 3*10^4)

defun SS such that
	+ SS \in {String} and
	+ there exist (i,j,k) such that
		(i \in N and j \in N and k \in N)
		and
		(i<j<k<n) and S[i] = SS[0] and S[j] = SS[1] and S[k] = SS[2]
defun x is |{SS}|
find x


----------------------

'''

Submission status: Accepted

Solution code:
n=int(input())
S=input()

ans=0
for i in range(1000):
  i=str(i).zfill(3)
  idx=S.find(i[0])
  if idx==-1: continue
  idx=S.find(i[1],idx+1)
  if idx==-1: continue
  idx=S.find(i[2],idx+1)
  if idx==-1: continue
  ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
count = 0

for i in range(1000):
    num = str(i).zfill(3)
    if num[0] in s:
        a1 = s.index(num[0])
        if num[1] in s[a1+1:]:
            a2 = a1+s[a1+1:].index(num[1])+1
            if num[2] in s[a2+1:]:
                count += 1

print(count)

Submission status: Accepted

Solution code:
N=int(input())
S=input()


nums=set()
numsidx=[-1 for i in range(10)]
for i in range(N-1,1,-1):
    if not S[i] in nums:
        nums.add(S[i])
        numsidx[int(S[i])]=i


nunums=set()
nunumsidx=[-1 for i in range(100)]
for i in range(N-2,0,-1):
    for m in range(10):
        if i<numsidx[m]:
            s=S[i]+str(m)
            if not s in nunums:
                nunums.add(s)
                nunumsidx[int(s)]=i

nums=set()
ans=set()
for i in range(N-2):
    if not S[i] in nums:
        nums.add(S[i])
        for m in range(100):
            if i<nunumsidx[m]:
                ans.add(S[i]+str(m))

print(len(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(1000):
  a=[str(i//100),str(i//10%10),str(i%10)]
  
  k=0
  b=0
  for j in range(n):
    if a[b]==s[j]:
      b+=1
    if b==3:
      k=1
      break
  if k:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
S = input()

d = defaultdict(list)
for i, s in enumerate(S):
    d[s].append(i)

ans = 0
for p in range(1000):
    p1, p2, p3 = str(p).zfill(3)

    i0 = -1
    for i in d[p1]:
        if i > i0:
            i1 = i
            break
    else:
        continue

    for i in d[p2]:
        if i > i1:
            i2 = i
            break
    else:
        continue

    for i in d[p3]:
        if i > i2:
            ans += 1
            break


print(ans)

Submission status: Accepted

Solution code:
import math


def inputIntList():
    return [int(s) for s in input().split()]


def inputInt():
    return int(input())


def main():
    N = inputInt()
    S = [int(s) for s in input()]

    ret = 0
    for i in range(1000):
        code = [int(i / 10 ** j) % 10 for j in range(2, -1, -1)]
        # print(code)
        j = 0
        for s in S:
            if code[j] == s:
                j += 1
            if j == 3:
                break
        if j == 3:
            ret += 1
    return ret


if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
N = int(input())
S = input()

cnt = 0
for i in range(1000):
  c = str(i).zfill(3)
  if c[0] in S:
        index1 = S.index(c[0])
        if c[1] in S[index1+1:]:
            index2 = index1 + 1 + S[index1+1:].index(c[1])
            if c[2] in S[index2+1:]:
                cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

key = []
for i in range(10):
    for j in range(10):
        for k in range(10):
            key.append([i,j,k])

ans=0
for i in range(1000):
    if str(key[i][0]) in S:
        ind_0=S.index(str(key[i][0]))
        if str(key[i][1]) in S[ind_0+1:]:
            ind_1=ind_0 + 1 + S[ind_0+1:].index(str(key[i][1]))
            if str(key[i][2]) in S[ind_1+1:]:
                ans+=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=0
for i in range(1000):
    num=str(i).zfill(3)
    x=s.find(num[0])
    y=s.find(num[1],x+1)
    z=s.find(num[2],y+1)
    if x!=-1 and y!=-1 and z!=-1:
        cnt +=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
cnt=0
for i in range(1000):
    num=str(i).zfill(3)
    base=s
    for k,j in enumerate(num):
        if j in base:base=base[base.index(j)+1:]
        else:break
        if k==2:cnt +=1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
 
##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力
 
# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと
 
##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順
 
# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    S = input()
    S = S[:-1]
    #print(S)
    
    ans = 0
    for i in range(1000):
        i_str = str(i)
        i_str = i_str.zfill(3)
        
        flg1 = True
        cnt = 0
        for i in i_str:
            flg2 = False
            for j in range(cnt, len(S)):
                if i == S[j]:
                    flg2 = True
                    cnt = j+1
                    break
            if flg2 == False:
                flg1 = False
                break
                
        if flg1 == True:
            #print(i_str)
            ans += 1
            
    print(ans)
    
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
# N桁のSからN-3桁の数字を削除して(N<30000)３桁の数字を残す．残った数字の組み合わせて押してありえるのは何通りか？
# 実際に10^4通りから３通り選ぶのは 10^4C3通りくらい計算量的には計算できる?
# 更にあり得る組み合わせの上限==1000通りを終えた時点で終了すれば更に早い？
# 右の数字から数えていって1を取れるかみたいにしていけば最大１０００のループで終わる？
# 0001がNの中から取れるかの判定まず最初の0について左側からとっていって，0があれば存在すればとり，そこのindexを記録，．次に1として次のindexから検索していく

#for n in N_str:
#なぜ224とかの時に上手く行かないのだ?
#0224
count=0
NUM = [str(i)+str(j)+str(k) for i in range(10) for j in range(10) for k in range(10)]
for i in NUM:
    i_3d = i
    S_mid = S
    for j,i_s in enumerate(i_3d): # 1,2
        if i_s in S_mid: # S_mid中にi_sがそんざいしていたら
            ind = S_mid.index(i_s)
            if j == len(i_3d)-1:  # 最後の文字がマッチングした
                count+=1
                break # break はforとwhileを抜け出すのに使われる
            elif ind+1 == len(S): # 文字はマッチングしたが最後の文字ではなく次の探索ができない場合
                break
            S_mid = S_mid[ind+1:]
        else: # S_mid中に存在しない
            break
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#三井住友信託銀行プログラミングコンテスト2019 D

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def C(n,r):
    return math.factorial(n)//math.factorial(n-r)//math.factorial(r)

n = I()
s = input()
t = list(map(int,list(s)))
ans = 0
for i in range(10):
    for j in range(n):
        if i == t[j]:
            tmp = j
            break
    else:
        continue
    for j in range(100):
        if len(str(j)) == 2:
            x,y = list(str(j))
        else:
            x,y = '0',str(j)
        flg = False
        for k in range(tmp+1,n):
            if flg and y == str(t[k]):
                ans += 1
                break
            if x == str(t[k]):
                flg = True
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

dp1 = [set() for i in range(4)]
dp2 = [set() for i in range(4)]
dp1[0].add('')
for i, S_i in enumerate(S):
    for j, dp1_j in enumerate(dp1[:3]):
        for s in dp1_j:
            dp2[j].add(s)
            dp2[j + 1].add(s + S_i)
        dp2[3] = dp1[3]
    dp1 = dp2
    dp2 = [set() for i in range(4)]

print(len(dp1[-1]))

Submission status: Accepted

Solution code:
n = int(input())
s = input()

ans = 0

for i in range(1000):
    ss = str(i).zfill(3)
    if (ss[0] in s):
        ind1 = s.index(ss[0])
        if (ss[1] in s[ind1+1:]):
            ind2 = ind1+1 + s[ind1+1:].index(ss[1])
            if (ss[2] in s[ind2+1:]):
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
new_s = input()
pas = -1

ans = 0
lens = len(new_s)
for i in range(1000):
    ott = 0
    pas = int(pas)+1
    if pas < 10:
        pas = "00"+str(pas)
    elif pas < 100:
        pas = "0"+str(pas)
    pas = str(pas)
    idx = -1
    while idx < lens-1:
        idx += 1
        if ott == 0:
            if pas[0] == new_s[idx]:
                ott += 1
        elif ott == 1:
            if pas[1] == new_s[idx]:
                ott += 1
        elif ott == 2:
            if pas[2] == new_s[idx]:
                ott += 1
                ans += 1
        if ott == 3:
            break
print(ans)

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
S = sys.stdin.buffer.readline().decode().rstrip()

dp = [set() for _ in range(3)]
for i, c in enumerate(S):
    for s in dp[1]:
        dp[2].add(s + c)
    for s in dp[0]:
        dp[1].add(s + c)
    dp[0].add(c)
print(len(dp[2]))

Submission status: Accepted

Solution code:
n = int(input())
S = input()
res = 0
for i in range(1000) :
    T = str(i).zfill(3)
    a = 0
    b = 0
    f1 = 0
    f2 = 0
    f = 0
    for j,t in enumerate(T) :
        if f :
            break
        for k,s in enumerate(S) :
            if t == s :
                if not f1 :
                    f1 = 1
                    a = k
                    break
                elif f1 and not f2 and a < k :
                    f2 = 1
                    b = k
                    break
                elif f1 and f2 and b < k :
                    res += 1
                    f = 1
                    break
print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lst = []
count = 0
for i in range(1000):
    num = str(i).zfill(3)
    if num[0] in S:
        a1 = S.index(num[0])
        if num[1] in S[a1+1:]:
            a2 = S[a1+1:].index(num[1]) + a1+1
            if num[2] in S[a2+1:]:
                count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
data = set()
xd,yd = set(),set()
for i in range(N-2):
  x = S[i]
  if x in xd:
    continue
  else:
    xd.add(x)
    yd = set()
  for j in range(i+1,N-1):
    y = S[j]
    if y in yd:
      continue
    else:
      yd.add(y)
    for k in range(j+1,N):
      z = S[k]
      data.add(x+y+z)
print(len(data))

Submission status: Accepted

Solution code:
import copy

N = input()
S = input()

key_list = range(0, 1000)
key_list = tuple(map(lambda key: '0' * (3 - len(str(key))) + str(key), key_list))

ans_list = []
for key in key_list:
    flg = True
    cS = copy.copy(S)
    for i in key:
        index = cS.find(i)
        if index == -1:
            flg = False
            break
        cS = cS[index+1:]
    if flg:
        ans_list.append(key)
        
print(len(ans_list))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
s=list(S)

count=0

for i in range(1000):
    ss=s[:]
    st=str(i).rjust(3,"0")
    if st[0] in ss:
        del ss[0:ss.index(st[0])+1]
        if st[1] in ss:
            del ss[0:ss.index(st[1])+1]
            if st[2] in ss:
                count+=1

print(count)

Submission status: Accepted

Solution code:
n = input()
sl = input()
ans = 0
for i in range(1000):
    i_zero = str(i)
    i_zero = i_zero.zfill(3)
    if i_zero[0] in sl:
        a = sl.index(i_zero[0])+1
        if i_zero[1] in sl[a:]:
            a = sl[a:].index(i_zero[1])+a+1
            if i_zero[2] in sl[a:]:
                ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = input()
#X = "196341362415710923461093419237419237417234162348162390471092374190237410982374901"*300
#N = len(X)
M = { }
A = set()
A.add(X[0]+X[1])
for i in range(2, N) :
    i = X[i]
    for j in tuple(A):
        M[j + i] = 1
        A.add(j[0]+i)
        A.add(j[1]+i)

    if len(M) == 1000:
        break
print(len(M))

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,list(input())))
ans=0
for i in range(10):
    try:
        ss=s[s.index(i)+1:]
        for j in range(10):
            try:
                sss=ss[ss.index(j)+1:]
                for k in range(10):
                    if k in sss:ans+=1
            except:pass
    except:pass

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num_list = [str(i).zfill(3) for i in range(1000)]
ans = 0
for i in range(1000):
    i100 = num_list[i][0]
    i10 = num_list[i][1]
    i1 = num_list[i][2]
    count = 0
    a = s.find(i100)
    if a != -1:
        count += 1
        a += 1
    b = s.find(i10, a)
    if b != -1:
        count += 1
        b += 1
    c = s.find(i1, b)
    if c != -1:
        count += 1
    if count == 3:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = input()
count = 0
num_ind_list = []
for i in range(10):
    num_ind_list.append([0])
for i in range(n):
    num_ind_list[int(s[i])].append(i)
for i in range(10):
    num_ind_list[i].append(30000)
for i in range(10):
    if len(num_ind_list[i]) != 2:
        for j in range(10):
            b = bisect.bisect_right(num_ind_list[j], num_ind_list[i][1])
            if num_ind_list[j][b] != 30000:
                for k in range(10):
                    c = bisect.bisect_right(num_ind_list[k], num_ind_list[j][b])
                    if num_ind_list[k][c] != 30000: count += 1
print(count)