## https://yukicoder.me/problems/no/1951 class BinaryIndexTree: """ フェニック木(BinaryIndexTree)の基本的な機能を実装したクラス """ def __init__(self, size): self.size = size self.array = [0] * (size + 1) def add(self, x, a): index = x while index <= self.size: self.array[index] += a index += index & (-index) def sum(self, x): index = x ans = 0 while index > 0: ans += self.array[index] index -= index & (-index) return ans def least_upper_bound(self, value): if self.sum(self.size) < value: return -1 elif value <= 0: return 0 m = 1 while m < self.size: m *= 2 k = 0 k_sum = 0 while m > 0: k0 = k + m if k0 < self.size: if k_sum + self.array[k0] < value: k_sum += self.array[k0] k += m m //= 2 if k < self.size: return k + 1 else: return -1 def main(): N = int(input()) S = input() s_array = [ord(s) - ord("A") for s in S] total_num = [0] * 26 shift_num = 0 for s in s_array: total_num[s] += 1 agct_num = 0 for s in ("A", "G", "C", "T"): agct_num += total_num[ord(s) -ord("A")] bit = BinaryIndexTree(N) for i in range(N): bit.add(i + 1, 1) count = 0 while agct_num > 0: index = bit.least_upper_bound(agct_num) target_s = s_array[index - 1] target_s += shift_num target_s %= 26 total_num[target_s] -= 1 n = total_num[target_s] bit.add(index, -1) shift_num += n shift_num %= 26 new_total_num = [0] * 26 for i in range(26): new_total_num[(i + n) % 26] = total_num[i] total_num = new_total_num agct_num = 0 for s in ("A", "G", "C", "T"): agct_num += total_num[ord(s) -ord("A")] count += 1 print(count) if __name__ == "__main__": main()