352 传统题

[ABC485D] 三分三全字符段 (ABC|ABC|ABC)

时间限制
2000ms
内存限制
1024MiB
登顶 / 尝试
2/ 2
难度
3
    上传者 标签AtCoderABC 485普及双指针动态规划

[ABC485D] 三分三全字符段 (ABC|ABC|ABC)

Score : 400400 points

Problem Statement

You are given a string SS of length NN consisting of A, B, and C.

We split SS at two positions to divide it into three non-empty substrings. Formally, let SkS_k denote the kk-th character of SS; we choose a pair of integers (i,j)(i, j) such that 1≤i<j<N1 \leq i < j < N, and divide SS into three substrings S1S2…SiS_1S_2 \ldots S_i, Si+1Si+2…SjS_{i + 1}S_{i + 2} \ldots S_j, and Sj+1Sj+2…SNS_{j + 1}S_{j + 2} \ldots S_N.

Find the number of ways to split SS such that each of the three substrings contains all of A, B, and C.

Constraints

  • 3≤N≤3×1053 \leq N \leq 3 \times 10^5

  • NN is an integer.

  • SS is a string of length NN consisting of A, B, and C.

Input

The input is given from Standard Input in the following format:

$N$
$S$

Output

Print the answer.

Sample Input 1

12
AABCCABCCBCA

Sample Output 1

5

Using | to denote the positions where the string is split, there are five ways to split it: AABC|CAB|CCBCA, AABC|CABC|CBCA, AABC|CABCC|BCA, AABCC|ABC|CBCA, and AABCC|ABCC|BCA.

Sample Input 2

3
ABC

Sample Output 2

0

Sample Input 3

20
BCACABCACACABCAACBCA

Sample Output 3

65