import typing import collections import sys def is_shuffled_palindrome(text: str) -> bool: if len(text) % 2: return True count = collections.Counter(text) if count['a'] % 2 == 0 and count['b'] % 2 == 0: return True return False def opposite(ch: str) -> str: return 'a' if ch == 'b' else 'b' def move_to_left(text: str, end: int, start: int) -> typing.Tuple[str, int]: num_moves = start - end text = ''.join([text[:end], text[start], text[end], text[end+1:start], text[start+1:]]) return text, num_moves def move_to_right(text: str, start: int, end: int) -> typing.Tuple[str, int]: num_moves = end - start text = ''.join([text[:start], text[start+1:end], text[end], text[start], text[end+1:]]) return text, num_moves def solve(text: str) -> int: if len(text) == 1: return 0 # a or b if not is_shuffled_palindrome(text): return -1 result = 0 length = len(text) for i in range(length // 2): left, right = text[i], text[length-1-i] if left != right: left_index = text.find(opposite(left), i) left_distance = left_index - i right_index = text.rfind(opposite(right), i, length-1-i) right_distance = length-1-i - right_index if left_distance <= right_distance: text, num_moves = move_to_left(text, i, left_index) else: text, num_moves = move_to_right(text, right_index, length-1-i) result += num_moves else: return result def main(): input = sys.stdin.read().strip() result = solve(input) sys.stdout.write(str(result)) sys.stdout.flush() if __name__ == '__main__': main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import typing import collections import sys def is_shuffled_palindrome(text: str) -> bool: if len(text) % 2: return True count = collections.Counter(text) if count['a'] % 2 == 0 and count['b'] % 2 == 0: return True return False def opposite(ch: str) -> str: return 'a' if ch == 'b' else 'b' def move_to_left(text: str, end: int, start: int) -> typing.Tuple[str, int]: num_moves = start - end text = ''.join([text[:end], text[start], text[end], text[end+1:start], text[start+1:]]) return text, num_moves def move_to_right(text: str, start: int, end: int) -> typing.Tuple[str, int]: num_moves = end - start text = ''.join([text[:start], text[start+1:end], text[end], text[start], text[end+1:]]) return text, num_moves def solve(text: str) -> int: if len(text) == 1: return 0 # a or b if not is_shuffled_palindrome(text): return -1 result = 0 length = len(text) for i in range(length // 2): left, right = text[i], text[length-1-i] if left != right: left_index = text.find(opposite(left), i) left_distance = left_index - i right_index = text.rfind(opposite(right), i, length-1-i) right_distance = length-1-i - right_index if left_distance <= right_distance: text, num_moves = move_to_left(text, i, left_index) else: text, num_moves = move_to_right(text, right_index, length-1-i) result += num_moves else: return result def main(): input = sys.stdin.read().strip() result = solve(input) sys.stdout.write(str(result)) sys.stdout.flush() if __name__ == '__main__': main() |