QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100 Difficulty: [show]

#8223. 字符串游戏

統計

Description

Little P and Little B like to play games, and they found skip. skip told them about the following game:

  • There is a string $S$ containing lowercase letters. At the start of the game, it is a string $S_0$ given by skip. The game calculates scores for Little P and Little B, both starting at $0$.
  • Little P and Little B take turns operating on this string, with Little P going first. In each turn, the current player can perform the following operation:
    • Choose a non-empty prefix of $S$ (which can be $S$ itself), gain a score equal to the number of occurrences of this prefix in $S$, and remove this prefix from $S$.
  • If $S$ becomes empty after an operation, the game ends.

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

  • Initially, $S_0 = ababa$;
  • Little P chooses the prefix $a$ of $ababa$, gains 3 points, and $S$ becomes $baba$;
  • Little B chooses the prefix $ba$ of $baba$, gains 2 points, and $S$ becomes $ba$;
  • Little P chooses $ba$, gains 1 point, the string becomes empty, and the game ends. Finally, Little P gains 4 points, and Little B gains 2 points.

Little P wants to maximize the difference between Little P's score and Little B's score, while Little B wants to minimize this value. They want to know what the final difference between Little P's score and Little B's score will be, assuming both players play optimally.

Input

Read from standard input.

The input consists of a single line containing a string $S_0$ composed of lowercase letters.

Output

Output to standard output.

A single integer representing the value of Little P's score minus Little B's score, assuming both players play optimally.

Examples

Example 1

Input

ababa

Output

2

Note

The optimal strategies for Little P and Little B are those given in the problem description.

Subtasks

Let $n$ be the length of the string $S$. For all test cases, $1 \le n \le 10^6$, and the string $S$ consists of lowercase letters.

Subtask ID$n \le$Special PropertiesScore
1$5\times 10^3$None10
2$10^6$Each character of $S$ is chosen independently and uniformly at random from $\{a,b\}$
3Each character of $S$ is $a$5
4$2 \times 10^5$Each character of $S$ is $a$ or $b$25
5$5 \times 10^5$None
6$10^6$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.