#P3564. [POI 2014] BAR-Salad Bar

    ID: 4401 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>2014二分单调队列POI(波兰)

[POI 2014] BAR-Salad Bar

Problem Description

Bytea went to a salad bar.

There are nn fruits lined up on the bar counter.

Specifically, these are apples and oranges.

Bytea can pick any contiguous segment of the line of fruits for her salad.

The fruits she chooses will be added to the salad either from left to right or from right to left.

As Bytea loves oranges, she requires that throughout the salad-making process, the number of oranges in the salad should always be no smaller than the number of apples, regardless of whether the fruits are added from left to right or from right to left.

Help Bytea by writing a program that finds the longest contiguous segment of the line of fruits that satisfies her requirements.

Input Format

The first line of the standard input contains a single integer nn (1n1 000 0001 \le n \le 1\ 000\ 000), denoting the number of fruits.

The next line contains a string of nn characters a1,a2,,ana_1, a_2, \cdots, a_n (ai{j,p}a_i \in \{j, p\}).

These stand for Polish names of apples and oranges: jab\l{ka} and pomara'ncze.

Consequently, if ai=ja_i = j, then the ii-th fruit in the line is an apple, and otherwise it is an orange.

Output Format

The first and only line of the standard output should contain a single integer equal to the number of fruits in the longest contiguous segment that satisfies Bytea's requirements.

Note that it could be the case that 00 is the correct result.

6
jpjppj

4

Hint

Given a string of length nn consisting only of 'p' and 'j', find the longest substring such that, whether you take it from left to right or from right to left, at every moment the number of 'p' taken is no less than the number of 'j'.

Translated by ChatGPT 5