Created
March 25, 2024 09:22
-
-
Save maehrm/6de7e891d31851c30350f44025719328 to your computer and use it in GitHub Desktop.
047 - Monochromatic Diagonal(★7) https://atcoder.jp/contests/typical90/tasks/typical90_au
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = int(input()) | |
S = input() | |
T = input() | |
MOD = 699999953 | |
seq1 = [0] * N | |
seq3 = [0] * N | |
for i in range(N): | |
if S[i] == "G": | |
seq1[i] = 1 | |
elif S[i] == "B": | |
seq1[i] = 2 | |
if T[i] == "G": | |
seq3[i] = 1 | |
elif T[i] == "B": | |
seq3[i] = 2 | |
ans = 0 | |
for i in range(3): | |
seq2 = [0] * N | |
for j in range(N): | |
seq2[j] = (i - seq3[j] + 3) % 3 | |
power3, hash1, hash2 = 1, 0, 0 | |
for j in range(N): | |
hash1 = (hash1 * 3 + seq1[j]) % MOD | |
hash2 = (hash2 + power3 * seq2[N - j - 1]) % MOD | |
if hash1 == hash2: | |
ans += 1 | |
power3 = power3 * 3 % MOD | |
power3, hash1, hash2 = 1, 0, 0 | |
for j in range(N - 1): | |
hash1 = (hash1 + power3 * seq1[N - j - 1]) % MOD | |
hash2 = (hash2 * 3 + seq2[j]) % MOD | |
if hash1 == hash2: | |
ans += 1 | |
power3 = power3 * 3 % MOD | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment