Problem statement
If there are integers greater than or equal to $ 0 $ that meet the following conditions, output the smallest of them. If no such integer exists, print $ -1 $. It is exactly $ N $ digit in decimal notation. ($ 0 $ is an integer with $ 1 $ digits. For other integers, notation with 0 at the beginning is not allowed.) Counting from the left, the $ s_i $ digit is $ c_i $. $ (i = 1,2, ⋯, M) $
Constraints
All inputs are integers
From the problem statement, the integer to be calculated is 3 digits, so we will calculate by brute force.
N, M = map(int,input().split())
s = []
c = []
for i in range(M):
S, C = map(int,input().split())
s.append(S)
c.append(C)
for i in range(10 ** (N + 1)):
Str = str(i)
if len(Str) == N and all([Str[s[j] - 1] == str(c[j]) for j in range(M)]):
print(Str)
exit()
print(-1)
The if in the second for uses the all function. The all function returns True when all the elements of an object, such as lists and tuples, are true. Here, it is judged whether all the conditions are met.
Recommended Posts