#P1787. [入门赛 #22] 非众数 Hard Version

[入门赛 #22] 非众数 Hard Version

Background

This problem is the Hard Version of “Non-majority String”. Here 1n1051 \le n \le 10^5. Thanks to

https://www.luogu.com.cn/user/101694

Problem Description

Given a string ss of length nn, where ss is guaranteed to contain only lowercase letters, find the number of non-empty substrings of ss that are non-majority strings.

Definition: non-empty substring

Let sis_i denote the ii-th character of ss (1in1 \leq i \leq n). Choose any two integers i,ji, j (1ijn1 \leq i \leq j \leq n), and take si,si+1,,sjs_i, s_{i + 1}, \cdots, s_{j} in the original order to form a new string. This new string is called a non-empty substring of ss.
For example, when s=abcdes = \texttt{abcde}, ab\texttt{ab}, bcde\texttt{bcde}, c\texttt{c}, and abcde\texttt{abcde} are all non-empty substrings of ss, while acd\texttt{acd}, f\texttt{f}, ngioasd\texttt{ngioasd}, and " "\texttt{" "} are not.

Definition: non-majority string

If, in a string aa, the maximum frequency among all characters is not greater than a2\lfloor \frac{|a|}{2} \rfloor, then the string aa is called a non-majority string. Here x\lfloor x \rfloor denotes the greatest integer x\leq x, and a|a| denotes the length of aa.

Input Format

One line containing a string ss.

Output Format

One line containing an integer, the answer.

aabb

2

fqmdfnc

21

Hint

Sample 1 Explanation

Among them, ab\texttt{ab} and aabb\texttt{aabb} are non-majority non-empty substrings.

Constraints

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, and the string consists of lowercase letters.

Translated by ChatGPT 5