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 | import sys def re(a, b, c, i): if i > 1 and dp.get((a, b, c, new_arr[i – 1], new_arr[i – 2])) is not None: return if i == length: print(”.join(new_arr)) exit() if b > 0: if i == 0 or new_arr[i – 1] != ‘B’: new_arr[i] = ‘B’ if i > 1: dp[(a, b, c, new_arr[i – 1], new_arr[i – 2])] = True re(a, b – 1, c, i + 1) if c > 0: if i == 0 or (i == 1 and new_arr[i – 1] != ‘C’) or (i > 1 and new_arr[i – 1] != ‘C’ and new_arr[i – 2] != ‘C’): new_arr[i] = ‘C’ if i > 1: dp[(a, b, c, new_arr[i – 1], new_arr[i – 2])] = True re(a, b, c – 1, i + 1) if a > 0: new_arr[i] = ‘A’ if i > 1: dp[(a, b, c, new_arr[i – 1], new_arr[i – 2])] = True re(a – 1, b, c, i + 1) arr = list(sys.stdin.readline().rstrip()) length = len(arr) new_arr = [0] * length dp = dict() a_num = 0 b_num = 0 c_num = 0 for i in arr: if i == ‘A’: a_num += 1 continue if i == ‘B’: b_num += 1 continue c_num += 1 re(a_num, b_num, c_num, 0) print(–1) | cs |