Created
March 23, 2024 07:01
-
-
Save maehrm/50c2a733acde64082b25e6ac525d86a2 to your computer and use it in GitHub Desktop.
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
class RollingHash: | |
def __init__(self, str, base, mod): | |
self.base = base | |
self.mod = mod | |
self.pow = [1] * (len(str) + 1) | |
self.hash = [0] * (len(str) + 1) | |
for i in range(len(str)): | |
self.hash[i + 1] = (self.hash[i] * base + ord(str[i])) % mod | |
self.pow[i + 1] = self.pow[i] * base % mod | |
def get(self, l, r): | |
return (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.mod | |
base = 128 | |
mod = 998244353 | |
N = int(input()) | |
T = input() | |
rh = RollingHash(T, base, mod) | |
rrh = RollingHash(T[::-1], base, mod) | |
for i in range(N + 1): | |
if rh.get(0, i) == rrh.get(N - i, N) and rh.get(N + i, 2 * N) == rrh.get( | |
N, 2 * N - i | |
): | |
print(T[:i] + T[N + i :]) | |
print(i) | |
break | |
else: | |
print(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment