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:
from bisect import bisect_left
n = int(input())
ss = input()
ans = 0

d = {str(i):[] for i in range(10)}
for i,c in enumerate(ss):
    d[c].append(i)

for i in range(1000):
    a,b,c = str(i).zfill(3)
    if d[a] == [] or d[b] == [] or d[c] == []:
        continue
    x,z = d[a][0], d[c][-1]
    y = bisect_left(d[b], x+1)
    if y >= len(d[b]):
        continue
    tmp = [x, d[b][y], z]
    if tmp[0] < tmp[1] and tmp[1] < tmp[2]:
        ans += 1

print(ans)

Submission status: Accepted

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

ans = 0
# 000 ～　999 がsから作成できるか確認していく
for i in range(1000):
    p = str(i).zfill(3)

    s_index_100 = s.find(p[2])
    if s_index_100 == -1:
        continue

    s_index_10 = s.find(p[1], s_index_100+1)
    if s_index_10 == -1:
        continue

    s_index_1 = s.find(p[0], s_index_10+1)
    if s_index_1 == -1:
        continue
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
def ok(target, S):
    for t in target:
        if t in S:
            S = S[S.index(t)+1:]
        else:
            return False
    return True


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

for i in range(0, 999+1):
    target = str(i).zfill(3)
    ans += ok(target, S)

print(ans)

Submission status: Accepted

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

def can_made(string, num):
    flag = False
    start_ind = string.find(num[0])
    if start_ind >= 0:
        second_str = string[start_ind+1::]
        second_ind = second_str.find(num[1])
        if second_ind >= 0:
            third_str = second_str[second_ind+1::]
            if third_str.find(num[2]) >= 0:
                flag = True
    #if flag: print(second_ind, num)
    return flag


for i in range(1000):
    num = str(i).zfill(3)
    if can_made(s, num): ans += 1
print(ans)

Submission status: Accepted

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

import itertools

ans = 0
for x, y, z in itertools.product(range(10), repeat=3):
    pin = str(x) + str(y) + str(z)
    cur = 0
    for i in range(n):
        if s[i] == pin[cur]:
            cur += 1
            if cur == 3:
                break
    if cur == 3:
        ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=[[] for i in range(10)]
for i in range(n):
    j=int(s[i])
    l[j].append(i)
ans=0
for i in range(1000):
    i=str(i).rjust(3,"0")
    a,b,c=[int(j) for j in i]
    if l[a] and l[b] and l[c]:
        p,q=l[a][0],l[c][-1]
        if p<q:
            for j in l[b]:
                if p<j<q:
                    ans+=1
                    break
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import bisect
n = int(input())
d = defaultdict(list)
for i,j in enumerate(input()):
  j = int(j)
  d[j].append(i)
c = 0
for i in range(10):
  for j in range(10):
    for k in range(10):
      if d[i] != [] and d[j] != [] and d[k] != []:
        b = bisect.bisect_right(d[j],d[i][0])
        if b != len(d[j]) and d[j][b] < d[k][-1]:
          c += 1
print(c)

Submission status: Accepted

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

ans = 0
for i in range(10):
    n = s.find(str(i))
    if n == -1:
        continue
    for j in range(10):
        m = s.find(str(j), n+1)
        if m == -1:
            continue
        for k in range(10):
            if str(k) in s[m+1:]:
                ans += 1
print(ans)

Submission status: Accepted

Solution code:
import itertools
import numpy as np
N = int(input())
S = input()

ind_table = [[] for _ in range(10)]
for i in range(N):
	ind_table[int(S[i])].append(i)

ind_table = np.array(ind_table)


num_list = ['1','2','3','4','5','6','7','8','9','0']

ans = 0
for nums in itertools.product(num_list,num_list,num_list):
	tmp = 0
	cond = 1
	for i in range(3):
		num = int(nums[i])
		L = len(ind_table[num])
		if L == 0:
			cond = 0
			break

		ind = np.searchsorted(ind_table[num], tmp)
		if ind == L:
			cond = 0
			break
		else:
			tmp = ind_table[num][ind] + 1

	if cond:
		ans += 1


print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def divisor(n):
        i = 1
        table = []
        while i * i <= n:
            if n % i == 0:
                table.append(i)
                table.append(n // i)
            i += 1
        table = list(set(table))
        return table


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)

    def list(self):
        return self.__q

    def length(self):
        return len(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


def main():
    # sys.stdin = open("Sample.txt")
    N = Scanner.int()
    S = Scanner.string()
    ans = 0
    for i in range(1000):
        s = str(i).rjust(3, "0")
        p = 0
        for j in range(N):
            if S[j] == s[p]:
                p += 1
                if p == 3:
                    break
        if p == 3:
            ans += 1
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

lst = [[] for _ in range(10)]
for i in range(n):
    lst[int(s[i])].append(i)

done = []
cnt = 0
for i in range(1000):
    tmp = str(i).rjust(3, '0')
    n1,n2,n3 = int(tmp[0]), int(tmp[1]), int(tmp[2])
    mi = -1
    mx = -1
    if len(lst[n1])!=0:
        mi = lst[n1][0]
    if len(lst[n3])!=0:
        mx = lst[n3][-1]
    
    if mi>=0 and mx>=0:
        for y in lst[n2]:
            if mi<y<mx:
                done.append(tmp)
    else:
        continue
print(len(set(done)))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_d

def main():
    N = int(input())
    S = tuple(map(int,input()))

    ans = 0

    # Rnum: 000 ~ 999
    for Rnum in range(1000):
        R = list(map(int,str(Rnum).zfill(3)))

        for s in reversed(S):
            if s == R[-1]:
                R.pop()
            if not R:
                ans += 1
                break

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_d

def main():
    N = int(input())
    S = tuple(map(int,input()))

    ans = 0

    # Rnum: 000 ~ 999
    for Rnum in range(1000):
        R = list(map(int,str(Rnum).zfill(3)))

        for s in S:
            if s == R[-1]:
                R.pop()
            if not R:
                ans += 1
                break

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:



N = int(input())
S = input()
"""
SからN-3個取り除くのは制限的にしんどい。nCr(40000,3)とかになるので
3桁の数字を作ればいいので、000~999が作れるか試す。
"""
ans = 0
for i in range(0,1000):
    pin = str(i).zfill(3)

    idx = 0
    for s in S:
        if pin[idx] == s:
            idx += 1

        if idx == 3:
            ans += 1
            break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
t=0
for i in range(10):
  for a in range(n):
    if s[a]==str(i):
      for j in range(10):
        for b in range(a+1,n)[::-1]:
          if s[b]==str(j):
            t+=len(set(list(s[a+1:b])))
            break
      break
print(t)

Submission status: Accepted

Solution code:
n=int(input())

s=input() 

ans = 0

for i in range(1000):
    count = 0
    i = str(i)
    i = (3-len(i))*'0'+i
    for j in range(n):
        if i[count] == s[j]:
            count += 1
        if count == 3:
            break
    if count == 3:
        ans += 1
print(ans)

Submission status: Accepted

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

lc = [0]
for c in S:
    lc.append(lc[-1] | (1<<int(c)))
rc = [0]
for c in S[::-1]:
    rc.append(rc[-1] | (1<<int(c)))
rc.reverse()

mem = [set() for _ in range(10)]
for c,l,r in zip(S,lc,rc[1:]):
    c = int(c)
    for i in range(10):
        if l&(1<<i)==0: continue
        for j in range(10):
            if r&(1<<j)==0: continue
            mem[c].add(10*i+j)
print(sum(len(m) for m in mem))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
S=[]
for i in range(n):
  S.append(int(s[i]))
ans=0
A=[0,1,2,3,4,5,6,7,8,9]
for i in range(n-2):
  if len(A)==0:
    break
  if S[i] not in A:
    continue
  B=[0,1,2,3,4,5,6,7,8,9]
  for j in range(i+1,n-1):
    if len(B)==0:
      break
    if S[j] not in B:
      continue
    C=[0,1,2,3,4,5,6,7,8,9]
    for k in range(j+1,n):
      if len(C)==0:
        break
      if S[k] not in C:
        continue
      ans=ans+1
      C.remove(S[k])
    B.remove(S[j])
  A.remove(S[i])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):

    result = 0
    for i in range(0, 9 + 1):
        first = S.find(str(i))
        if first == -1:
            # print(i)
            continue

        for j in range(0, 9 + 1):
            second = S.find(str(j), first + 1)
            if second == -1:
                # print(j)
                continue

            for k in range(0, 9 + 1):
                if S.find(str(k), second + 1) != -1:
                    result += 1

    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: int
    solve(N, S)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
Ss = list(input())
S = [int(i) for i in Ss]

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            val = [i,j,k]
            idx = 0
            for s in S:
                if s==val[idx]:idx+=1
                if idx>2:
                    ans += 1
                    break
print(ans)

Submission status: Accepted

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

def solve(n, s):
    # 全探索
    cnt = 0
    for v in range(0, 1000):
        v = str(v)
        v = "0" * (3 - len(v)) + v
        idx = 0
        for i in range(len(s)):
            if s[i] == v[idx]:
                idx += 1
            if idx == 3:
                cnt += 1
                break
    print(cnt)
solve(n, s)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s1 = [False for i in range(10)]
s2 = [False for i in range(100)]
s3 = [False for i in range(1000)]


for c in s:
    a = int(c)

    for i in range(100):
        if s2[i]:
            s3[10 * i + a] = True
    for i in range(10):
        if s1[i]:
            s2[10 * i + a] = True
    s1[a] = True

print(sum(s3))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
he, ta = {}, {}

for i in range(N):
  if not S[i] in he:
    he[S[i]] = i
  if len(he) >= 10:
    break

for i in range(N-1,-1,-1):
  if not S[i] in ta:
    ta[S[i]] = i
  if len(ta) >= 10:
    break
#print(he)
#print(ta)
ans = 0
for hek, hep in he.items():
  for tak, tap in ta.items():
    if hep>=tap:
      continue
    wk = set()
    for i in range(hep+1, tap):
      wk.add(S[i])
      if len(wk) >= 10:
        break
    ans += len(wk)
    #print(ans, wk, hek, tak)
print(ans)

Submission status: Accepted

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

ans=0
for num in range(10):
    found1=-1
    for i in range(len(s)):
        if s[i] == str(num):
            found1=i
            break
    if found1==-1:
        continue

    for num2 in range(10):
        found2=-1
        for j in range(found1+1,len(s)):
            if s[j]== str(num2):
                found2=j
                break
        if found2==-1:
            continue

        for num3 in range(10):
            found3=-1
            for k in range(found2+1,len(s)):
                if s[k]== str(num3):
                    found3=k
                    break
            if found3==-1:
                continue
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = input()
D = [[] for _ in range(10)]
for i in range(n):
  D[int(s[i])].append(i)
ans = 0
for i in range(10):
  for j in range(10):
    for k in range(10):
      if D[i] != []:
        l = D[i][0]
        t = bisect.bisect_right(D[j], l)
        if t < len(D[j]):
          m = D[j][t]
          q = bisect.bisect_right(D[k], m)
          if q < len(D[k]):
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline

N = int(input())
S = input()
cc = [0]*1000
ans = [0]*1000
for j in range(len(S)-1):
	p = int(S[j])
	for i in range(1000):
		if(p==i%10 and cc[i]==2):
			cc[i]+=1
			ans[i]=1
		elif(p==(i//10)%10 and cc[i]==1):
			cc[i]+=1
		elif(p==i//100 and cc[i]==0):
			cc[i]+=1
print(sum(ans))

Submission status: Accepted

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

for i in range(10):
    x = S.find(str(i))
    if x == -1 or x >= N - 2:
        continue
    for j in range(10):
        y = S.find(str(j), x + 1)
        if y == -1 or y == N - 1:
            continue
        for k in range(10):
            z = S.find(str(k), y + 1)
            if z != -1:
                count += 1
                
print(count)

Submission status: Accepted

Solution code:
a = int(input())
b = input()
ans = 0
for i in range(1000):
	T = '0'*(3-len(str(i)))
	t = T+ str(i)
	I = 0
	while I < 3:
		for j in b:
			#if t == '022':print(I,t[I],j)
			if I == 3:
				break
			if t[I] == j:I+=1
			#if t == '022':print(I,j)
		
		if I == 3: 
			#print(t)
			ans +=1
		break
print(ans)

Submission status: Accepted

Solution code:
N = int(input()) 
S = input()[::-1]

# 下の桁を固定した時上の桁のとりうる値を調べる
# 10のループが2つ回るので計算量は O(10^6)

ret = 0
# 1の位を固定して残りの桁の可能性を調べる
place1 = [True for i in range(10)]
for i, s in enumerate(S):
  if i > N - 3:
    break
  if place1[int(s)]:
    place1[int(s)] = False
    
    # 10の位を固定して残りの桁の可能性を調べる
    place2 = [True for j in range(10)]
    for j, t in enumerate(S[i+1:], i+1):
      if j > N - 2:
        break
      if place2[int(t)]:
        place2[int(t)] = False
      
        # 100の位は残ったユニークな数字の数
        ret += len(set(S[j+1:]))
        
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(1000):
    i = str(i).zfill(3)
    j = s.find(i[0])
    if j != -1:
        k = s[j + 1:].find(i[1])
        if k != -1:
            if s[j + k + 2:].find(i[2]) != -1:
                cnt += 1
print(cnt)

Submission status: Accepted

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

for i in range(10):
    ind_i = S.find(str(i))
    if ind_i != -1:
        S2 = S[ind_i + 1:]
        for j in range(10):
            ind_j = S2.find(str(j))
            if ind_j != -1:
                S3 = S2[ind_j + 1:]
                for k in range(10):
                    cnt_k = S3.count(str(k))
                    if cnt_k > 0:
                        ans += 1


print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

N = int(input())
S = input()
keta = 3
count = 0
for i in range(1000):
    s = str(i).zfill(keta)
    pos = -1
    for j in range(keta):
        tmp = S[pos+1:].find(s[j])
        if tmp == -1:
            break
        pos += tmp + 1
    else:
        count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = list(str(input()))
result = 0
for i in range(len(S)):
    S[i] = int(S[i])
    
for i in range(10):
    if S.count(i) >= 1:
        val = S.index(i)
        S_2 = S[val+1:]
        for j in range(10):
            if S_2.count(j) >= 1:
                val_2 = S_2.index(j)
                S_3 = S_2[val_2+1:]
                for k in range(10):
                    if S_3.count(k) >= 1:
                        result += 1

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def z_algorithm(S):
    N = len(S)
    Z = [0]*N

    c = 0
    for i in range(1, N):
        if i+Z[i-c] < c+Z[c]:
            Z[i] = Z[i-c]
        else:
            j = max(0, c+Z[c]-i)
            while i+j < N and S[j] == S[i+j]:
                j += 1
            Z[i] = j
            c = i
    Z[0] = len(S)
    return Z


def zfindall(text, pattern, separator="$"):
    N = len(pattern)
    conc = pattern+separator+text
    Z = z_algorithm(conc)

    umu = [i-N-1 for i in range(len(conc)) if Z[i] == N]
    return umu


def solve(N: int, S: str):

    len1 = [0]*10
    len2 = [0]*100
    len3 = [0]*1000
    for c in S:
        # 長さ3
        for i in range(100):
            if len2[i] > 0:
                len3[int(str(i)+c)] = 1

        # 長さ2
        for i in range(10):
            if len1[i] > 0:
                len2[int(str(i)+c)] = 1

        # 長さ1
        len1[int(c)] = 1
    counter = 0
    for i in range(1000):
        if len3[i] == 1:
            counter += 1

    print(counter)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    N = I()
    s = S()
    t = [set() for _ in range(4)]
    t[0].add("")
    for c in s:
        for i in range(2,-1,-1):
            for k in t[i]:
                t[i+1].add(k+c)

    return len(t[3])


print(main())

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int,list(input().strip())))
cnt = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            cur = [i,j,k]
            ind = 0
            for n in range(N):
                if S[n]==cur[ind]:
                    ind += 1
                    if ind==3:
                        cnt += 1
                        break
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
numbers=[str(i).zfill(3) for i in range(1000)]

count=0
for num in numbers:
	j=0
	for i in range(n):
		if num[j]==s[i]:
			j+=1
		if j==3:
			break
	if j==3:
		count+=1
print(count)

Submission status: Accepted

Solution code:
def search(num, moji):
    num2str = str(num)
    for i, n in enumerate(moji):
        if n == num2str:
            return moji[i+1:], True
    return "", False


a = int(input())
b = input()
buf = b[:]
result = 0
for i in range(10):
    buf, flag = search(i, b[:])
    if flag:
        for j in range(10):
            buf2, flag2 = search(j, buf)
            if flag2:
                for k in range(10):
                    _, flag3 = search(k, buf2)
                    if flag3:
                        result += 1
print(result)

Submission status: Accepted

Solution code:
def Lucky_PIN():
	n=int(input())
	s=input()
	ans=0
	for i in range(1000):
		p='{:03d}'.format(i)
		n1=s.find(p[0])
		if n1!=-1:
			n2=s[n1+1:].find(p[1])
			if n2!=-1:
				n3=s[n1+n2+2:].find(p[2])
				if n3!=-1:
					ans+=1
	print(ans)
if __name__=='__main__':
	Lucky_PIN()

Submission status: Accepted

Solution code:
#三井住友信託銀行プログラミングコンテスト2019-D
N = int(input())
S = input()
ans = 0

possible_pass = [str(i).zfill(3) for i in range(1000)]

for password in possible_pass:
    try:
        first_num = S.index(password[0])
        S_second = S[first_num+1:]
        second_num = S_second.index(password[1])
        S_third = S_second[second_num+1:]
        third_num = S_third.index(password[2])
        ans += 1
    except:
        continue
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
password = [0, 0, 0]
for i in range(1000):
    for j in range(3):
        password[2 - j] = i // (10 ** j) % 10
    if str(password[0]) in s:
        index = s.index(str(password[0]))
        if str(password[1]) in s[index + 1:]:
            index2 = index + 1 + s[index + 1:].index(str(password[1]))
            if str(password[2]) in s[index2 + 1:]:
                ans += 1
print(ans)

Submission status: Accepted

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

ans = 0

for i in range(1000):
        target=str(i).zfill(3)
        d=0
        for j in range(N):
            if target[d]==S[j]:
                d+=1
                if d==3:
                    ans+=1
                    break
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
ans=0
A=["0","1","2","3","4","5","6","7","8","9"]
for i in range(n-2):
  if len(A)==0:
    break
  elif S[i] in A:
    A.remove(S[i])
  else:
    continue
  B=["0","1","2","3","4","5","6","7","8","9"]
  for j in range(i+1,n-1):
    if len(B)==0:
      break
    elif S[j] in B:
      B.remove(S[j])
    else:
      continue
    C=["0","1","2","3","4","5","6","7","8","9"]
    for k in range(j+1,n):
      if len(C)==0:
        break
      elif S[k] in C:
        C.remove(S[k])
        ans=ans+1
      else:
        continue
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from bisect import *

N = int(input())
S = input()
idx = defaultdict(list)

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

ans = 0

for i in range(1000):
    n1 = i//100
    n2 = (i-n1*100)//10
    n3 = (i-n1*100-n2*10)
    
    if len(idx[n1])==0:
        continue
    
    m1 = bisect_right(idx[n2], idx[n1][0])
    
    if m1==len(idx[n2]):
        continue
    
    m2 = bisect_right(idx[n3], idx[n2][m1])
    
    if m2==len(idx[n3]):
        continue
    
    ans += 1

print(ans)

Submission status: Accepted

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

alph = "0123456789"
next_ = [[-1] * (n + 1) for i in range(len(alph))]
for ai in range(len(alph)):
    tmp = - 1
    for si in range(n)[::-1]:
        if s[si] == alph[ai]:
            tmp = si + 1
        next_[ai][si] = tmp

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            tmp = next_[i][0]
            tmp = next_[j][tmp]
            tmp = next_[k][tmp]
            if tmp != -1:
                ans += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
indexs=[[] for i in range(10)]
for i in range(N):
  indexs[int(S[i])].append(i)

counter=0
for i in range(0, 1000):
  a=i//100
  b=(i%100)//10
  c=i%10

  if len(indexs[a])>0 and len(indexs[b])>0 and len(indexs[c])>0:
    x=min(indexs[a])
    z=max(indexs[c])
    n=len(indexs[b])
    j=0
    for i in range(n):
      if indexs[b][i]>x and indexs[b][i]<z:
        j=1
    counter+=j

print(counter)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    cnt=0
    for i in range(1000):
        i="".join(["00",str(i)])[-3:]
        now=0
        char=i[0]
        for c in s:
            if char==c:
                if now<2:
                    now+=1
                    char=i[now]
                else:
                    cnt+=1
                    break
    print(cnt)
main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
count=0
for i in range(1000):
    p='0'*(3-len(str(i)))+str(i)
    p1=S.find(p[0])
    if p1!=-1:
        p2=S[p1+1:].find(p[1])
        if p2!=-1:
            p3=S[p1+p2+2:].find(p[2])
            if p3!=-1:
                count+=1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(1000):
  i_str = str(i).zfill(3)
  j = 0
  for k in s:
    if k == i_str[j]:
      j += 1
      if j == 3:
        ans += 1
        break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
flag = False
ans = 0
for i in range(1000):
    p = str(i).zfill(3)
    a = [-1, -1, -1]
    for j in range(len(S)):
        if p[0] == S[j]:
            a[0] = j
            break
    if a[0] != -1:
        for k in range(a[0] + 1, len(S)):
            if p[1] == S[k]:
                a[1] = k
                break
    else:
        continue
    if a[1] != -1:
        for l in range(a[1] + 1, len(S)):
            if p[2] == S[l]:
                a[2] = l
                break
    else:
        continue
    if a[2] != -1:
        ans += 1

print(ans)

Submission status: Accepted

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

r = 0
for i in range(10):
  s = a.find(str(i))
  if s == -1:
    continue
  for j in range(10):
    s2 = a.find(str(j), s + 1)
    if s2 == -1:
      continue
    for k in range(10):
      if a.find(str(k), s2 + 1) != -1:
        r += 1
print(r)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L=[[0 for j in range(10)]for i in range(N+1)]
R=[[0 for j in range(10)]for i in range(N+1)]
for i in range(N):
    for j in range(10):
        L[i+1][j]=L[i][j]
    L[i+1][int(S[i])]=1
for i in range(N-1,-1,-1):
    for j in range(10):
        R[i][j]=R[i+1][j]
    R[i][int(S[i])]=1
ans=[[[0 for k in range(10)]for j in range(10)] for i in range(10)]
num=[0 for k in range(10)]
for t in range(N):
    X=[]
    Y=[]
    k=int(S[t])
    for x in range(10):
        if L[t][x]==1:
            X.append(x)
    for y in range(10):
        if R[t+1][y]==1:
            Y.append(y)
    for i in X:
        for j in Y:
            if ans[i][j][k]==0:
                ans[i][j][k]=1
                num[k]+=1
print(sum(num))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L=[[0 for j in range(10)]for i in range(N+1)]
R=[[0 for j in range(10)]for i in range(N+1)]
for i in range(N):
    for j in range(10):
        L[i+1][j]=L[i][j]
    L[i+1][int(S[i])]=1
for i in range(N-1,-1,-1):
    for j in range(10):
        R[i][j]=R[i+1][j]
    R[i][int(S[i])]=1
ans=[[[0 for k in range(10)]for j in range(10)] for i in range(10)]
num=[0 for k in range(10)]
for t in range(N):
    X=[]
    Y=[]
    k=int(S[t])
    if num[k]==100:
        continue
    for x in range(10):
        if L[t][x]==1:
            X.append(x)
    for y in range(10):
        if R[t+1][y]==1:
            Y.append(y)
    for i in X:
        for j in Y:
            if ans[i][j][k]==0:
                ans[i][j][k]=1
                num[k]+=1
print(sum(num))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
length = int(input())
string = input()

possible_pins = list()
for pin in [str(num) for num in range(0, 1000)]:
    if len(pin) == 1:
        pin = "00" + pin
    elif len(pin) == 2:
        pin = "0" + pin
    possible_pins.append(pin)


count = 0
for pin in possible_pins:
    start = 0
    for letter in pin:
        try:
            index = string.index(letter, start)
            start = index + 1
        except ValueError:
            break
    else:
        count += 1
print(count)

Submission status: Accepted

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

ans = 0
for i in range(1000):
  pin = '{:03d}'.format(i)
  p1 = s.find(pin[0])
  if p1>=0:    
    p2 = s.find(pin[1],p1+1)     
    if p2>=0:
      p3 = s.find(pin[2],p2+1)      
      if p3>=0:
        ans+=1        
    
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1000):
  pin = '{:03d}'.format(i)
  p1 = s.find(pin[0])
  if p1>=0:    
    p2 = s[p1+1:].find(pin[1])     
    if p2>=0:
      p3 = s[p1+p2+2:].find(pin[2])      
      if p3>=0:
        ans+=1        
    
print(ans)