#FT0000125. Chuỗi có triển vọng – phiên bản khó (Promising String (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chuỗi có triển vọng – phiên bản khó (Promising String (Hard Version))
Promising String (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A nonempty string of + and - characters is balanced if it contains equally many plus and minus signs. One operation replaces two adjacent minus signs (--) by a single plus sign (+). The string becomes one character shorter.
A nonempty string is promising if zero or more operations can make it balanced. Given a string of length , count all nonempty contiguous substrings that are promising. Equal-looking substrings at different positions count separately.
For example, --- is promising since --- can become +-, which is balanced, while -- is not. Noncontiguous subsequences do not count.
Input
- First line: , the string length.
- Second line: a string of exactly characters, each
+or-.
Output
Print the number of nonempty promising substrings as one integer.
Subtasks
- Subtask 1 (20 points): ; Strings consist only of plus and minus signs.
- Subtask 2 (30 points): ; Strings consist only of plus and minus signs.
- Subtask 3 (50 points): ; Strings consist only of plus and minus signs.
Examples
Example 1
Input
3
+-+
Output
2
Explanation
The two length-two substrings +- at positions 1–2 and -+ at positions 2–3 are already balanced. Neither a one-character substring nor +-+ can become balanced, so the answer is 2.
Example 2
Input
5
-+---
Output
4
Explanation
The four valid substrings are [1,2] = -+, [2,3] = +-, [3,5] = ---, and [1,5] = -+---. The first two are balanced, --- becomes +-, and -+--- becomes -++-. Thus the answer is 4.