QOJ.ac

QOJ

حد الوقت: 2.5 s حد الذاكرة: 128 MB مجموع النقاط: 100

#12397. 三座塔

الإحصائيات

Bythony 喜欢玩游戏。在他的房间里,他排列了一排 $n$ 个彩色积木。每个积木要么是白色、灰色,要么是黑色。Bythony 想要从这一排积木中选取一个连续的片段,并用其中的积木搭建塔。

每座塔只能由同一种颜色的积木组成,且塔的颜色必须各不相同(因此,Bythony 最多可以搭建三座塔)。此外,塔的高度(即它们所包含的积木数量)也必须各不相同。我们假设 Bythony 必须使用他所选片段中的所有积木。请编写一个程序,帮助他确定满足这些要求的最长连续积木片段的长度。

输入格式

输入的第一行包含一个整数 $n$ ($1 \le n \le 1\,000\,000$),表示积木的数量。下一行包含一个长度为 $n$ 的字符串 $a_1a_2 \dots a_n$,其中 $a_i$ 是字符 B、S 或 C 中的一个,表示第 $i$ 个积木的颜色:B 代表白色(biały),S 代表灰色(szary),C 代表黑色(czarny)。

在占总分 30% 的测试数据中,满足额外条件 $n \le 2500$。

输出格式

输出一行,包含一个整数,表示满足 Bythony 要求的最长片段的积木数量。

样例

输入格式 1

9
CBBSSBCSC

输出格式 1

6

说明

Bythony 可以选择一个包含 6 个积木的片段:BSSBCS,从中他可以搭建一座 3 个积木高的灰色塔、一座 2 个积木高的白色塔和一座 1 个积木高的黑色塔。

样例评分测试

  • 1ocen: $n = 2500$,积木排列如下:$\text{B}^{1248}\text{CSB}^{1250}$(字符串 $\text{B}^k$ 表示字符 B 重复 $k$ 次);Bythony 可以选择的最长片段已下划线标出。
  • 2ocen: $n = 1\,000\,000$,积木排列如下:$\text{BSCBSCBSC} \dots \text{BSCBSCB}$;Bythony 只能搭建一座高度为 1 的塔。

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.