QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 512 MB Points totaux : 100

#183. 黑白盒子

Statistiques

Alice 和 Bob 正在玩以下游戏:

  1. 游戏中有若干堆垂直摆放的盒子。盒子大小相同,颜色为黑色或白色。
  2. 两名玩家(Alice 和 Bob)轮流进行操作。谁先手由公平的随机抽签决定。
  3. Alice 回合时,她选择其中一堆中的一个黑色盒子,并将该盒子及其上方所有的盒子(如果有的话)移除。如果没有黑色盒子可供移除,她就输掉了游戏。
  4. Bob 回合时,他选择其中一堆中的一个白色盒子,并将该盒子及其上方所有的盒子(如果有的话)移除。如果没有白色盒子可供移除,他就输掉了游戏。

给定初始的堆配置以及谁先手,该游戏是一个确定的完美信息博弈。在这种游戏中,只要双方都采取最优策略,其中一名玩家必胜。因此,先手的抽签结果本质上决定了胜者。

事实上,这种看似枯燥的性质在许多流行游戏中都很常见,例如国际象棋。然而,国际象棋的复杂程度足以阻止彻底的分析,即使是超级计算机也无法做到,这为我们留下了享受游戏的余地。

然而,这个盒子堆游戏并没有那么复杂。最优策略更容易被发现。因此,初始配置应该是公平的,即给予两名玩家获胜的机会。如果一个配置无论谁先手,其中一方总是必胜,那么该配置就是不理想的。

你需要通过从给定的候选集合中挑选若干堆盒子,来安排该游戏的初始配置。由于更复杂的配置会让游戏更有趣,你需要找出在所有公平的配置中,盒子总数最多的那一个。

输入格式

输入包含单个测试用例,格式如下:

$n$ $p_1$ $\vdots$ $p_n$

正整数 $n$ ($\le 40$) 是候选堆的数量。每个 $p_i$ 是一个由字符 B 和 W 组成的字符串,代表第 $i$ 个候选堆。B 和 W 分别代表黑色和白色盒子。它们在字符串中出现的顺序即为堆中从底向上的顺序。每个候选堆中的盒子数量不超过 40。

输出格式

输出一行,表示由部分候选堆组成的公平初始配置中,盒子总数的最大值。如果只有空配置是公平的,则输出 0。

样例

样例输入 1

4
B
W
WB
WB

样例输出 1

5

样例输入 2

6
B
W
WB
WB
BWW
BWW

样例输出 2

10

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.