A15063 | Promising String (hard version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is the hard version of Problem F. The only difference between the easy version and the hard version is the constraints.
We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.
We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation:
- replace two adjacent minus signs with one plus sign.
In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.
For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".
How many non-empty substrings of the given string $s$ are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string $s$ .
Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++".
We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced.
We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation:
- replace two adjacent minus signs with one plus sign.
In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced.
For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+".
How many non-empty substrings of the given string $s$ are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string $s$ .
Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++".
输入格式
The first line of the input contains an integer $t$ ( $1 \le t \le 10^4$ ) —the number of test cases in the test.
Then the descriptions of test cases follow.
Each test case of input data consists of two lines. The first line consists of the number $n$ ( $1 \le n \le 2 \cdot 10^5$ ): the length of $s$ .
The second line of the test case contains the string $s$ of length $n$ , consisting only of characters "+" and "-".
It is guaranteed that the sum of values $n$ over all test cases does not exceed $2 \cdot 10^5$ .
Then the descriptions of test cases follow.
Each test case of input data consists of two lines. The first line consists of the number $n$ ( $1 \le n \le 2 \cdot 10^5$ ): the length of $s$ .
The second line of the test case contains the string $s$ of length $n$ , consisting only of characters "+" and "-".
It is guaranteed that the sum of values $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, print a single number: the number of the promising non-empty substrings of string $s$ . Each non-empty promising substring must be counted in the answer as many times as it occurs in string $s$ .
输入输出样例
输入 #1
5 3 +-+ 5 -+--- 4 ---- 7 --+---+ 6 +++---
输出 #1
2 4 2 7 4
The following are the promising substrings for the first three test cases in the example:
1. $s[1 \dots 2]$ ="+-", $s[2 \dots 3]$ ="-+";
2. $s[1 \dots 2]$ ="-+", $s[2 \dots 3]$ ="+-", $s[1 \dots 5]$ ="-+---", $s[3 \dots 5]$ ="---";
3. $s[1 \dots 3]$ ="---", $s[2 \dots 4]$ ="---".
1. $s[1 \dots 2]$ ="+-", $s[2 \dots 3]$ ="-+";
2. $s[1 \dots 2]$ ="-+", $s[2 \dots 3]$ ="+-", $s[1 \dots 5]$ ="-+---", $s[3 \dots 5]$ ="---";
3. $s[1 \dots 3]$ ="---", $s[2 \dots 4]$ ="---".
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted