C - Slot Strategy 2 (Easy) Editorial by evima

Easy solution

If you can’t match the numbers by the time the reels spin three times, you can’t forever. Thus, it is sufficient to try all possible ways to press each button at one of the times \(0, 1, ..., 3M - 1\).

Sample Implementation (Python)

M = int(input())
N = 3
S = [input() for _ in range(N)]
INF = 1e9
ans = INF
for i in range(N * M):
    for j in range(N * M):
        for k in range(N * M):
            if i != j and i != k and j != k and S[0][i % M] == S[1][j % M] == S[2][k % M]:
                ans = min(ans, max(i, j, k))
print(ans if ans < INF else -1)

posted:
last update: