#P10215. [CTS2024] 字符串游戏

[CTS2024] 字符串游戏

Problem Description

Xiao P and Xiao B like playing games. They found skip. skip told them about the following game:

  • There is a string SS consisting of lowercase letters. When the game starts, it is the string S0S_0 given by skip. The game scores points for Xiao P and Xiao B, and initially both of their scores are 00.
  • Xiao P and Xiao B take turns operating on this string, with Xiao P moving first. On each turn, the current player can do the following:
    • Choose a non-empty prefix of SS (it may be SS itself), gain points equal to the number of occurrences of this prefix in SS, and then delete this prefix from SS.
  • If after some operation SS becomes empty, the game ends.

To help you better understand the rules, consider the following example:

  • Initially, S0=ababaS_0 = ababa.
  • Xiao P chooses the prefix aa of ababaababa, gains 3 points, and SS becomes babababa.
  • Xiao B chooses the prefix baba of babababa, gains 2 points, and SS becomes baba.
  • Xiao P chooses baba, gains 1 point, the string becomes empty, and the game ends. Finally, Xiao P gets 4 points and Xiao B gets 2 points.

Xiao P wants to maximize (Xiao P's score minus Xiao B's score), while Xiao B wants to minimize this value. They want to know, assuming both players are perfectly smart, what the final value of (Xiao P's score minus Xiao B's score) will be.

Input Format

Read from standard input.

The input contains only one line: a string S0S_0 consisting of lowercase letters.

Output Format

Write to standard output.

Output one integer, representing the value of (Xiao P's score minus Xiao B's score) when the game ends, assuming both players are perfectly smart.

ababa

2

Hint

Sample 1 Explanation

The optimal strategies of Xiao P and Xiao B are the strategy given in the problem description.

Subtasks

Let nn be the length of the string SS. For all testdata, 1n1061 \le n \le 10^6, and the string SS consists of lowercase letters.

Subtask ID nn \le Special Property Points
1 5×1035 \times 10^3 None 10
2 10610^6 Each character of SS is independently uniform in {a,b}\{a,b\}
3 Every character of SS is aa 5
4 2×1052 \times 10^5 Every character of SS is aa or bb 25
5 5×1055 \times 10^5 None
6 10610^6

Translated by ChatGPT 5