A12881 | AB-string
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The string $t_1t_2 \dots t_k$ is good if each letter of this string belongs to at least one palindrome of length greater than 1.
A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.
Here are some examples of good strings:
- $t$ = AABBB (letters $t_1$ , $t_2$ belong to palindrome $t_1 \dots t_2$ and letters $t_3$ , $t_4$ , $t_5$ belong to palindrome $t_3 \dots t_5$ );
- $t$ = ABAA (letters $t_1$ , $t_2$ , $t_3$ belong to palindrome $t_1 \dots t_3$ and letter $t_4$ belongs to palindrome $t_3 \dots t_4$ );
- $t$ = AAAAA (all letters belong to palindrome $t_1 \dots t_5$ );
You are given a string $s$ of length $n$ , consisting of only letters A and B.
You have to calculate the number of good substrings of string $s$ .
A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.
Here are some examples of good strings:
- $t$ = AABBB (letters $t_1$ , $t_2$ belong to palindrome $t_1 \dots t_2$ and letters $t_3$ , $t_4$ , $t_5$ belong to palindrome $t_3 \dots t_5$ );
- $t$ = ABAA (letters $t_1$ , $t_2$ , $t_3$ belong to palindrome $t_1 \dots t_3$ and letter $t_4$ belongs to palindrome $t_3 \dots t_4$ );
- $t$ = AAAAA (all letters belong to palindrome $t_1 \dots t_5$ );
You are given a string $s$ of length $n$ , consisting of only letters A and B.
You have to calculate the number of good substrings of string $s$ .
输入格式
The first line contains one integer $n$ ( $1 \le n \le 3 \cdot 10^5$ ) — the length of the string $s$ .
The second line contains the string $s$ , consisting of letters A and B.
The second line contains the string $s$ , consisting of letters A and B.
输出格式
Print one integer — the number of good substrings of string $s$ .
输入输出样例
输入 #1
5 AABBB
输出 #1
6
输入 #2
3 AAA
输出 #2
3
输入 #3
7 AAABABB
输出 #3
15
In the first test case there are six good substrings: $s_1 \dots s_2$ , $s_1 \dots s_4$ , $s_1 \dots s_5$ , $s_3 \dots s_4$ , $s_3 \dots s_5$ and $s_4 \dots s_5$ .
In the second test case there are three good substrings: $s_1 \dots s_2$ , $s_1 \dots s_3$ and $s_2 \dots s_3$ .
In the second test case there are three good substrings: $s_1 \dots s_2$ , $s_1 \dots s_3$ and $s_2 \dots s_3$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted