Codeforces Round 721 (Div. 2)
B2
Palindrome Game (hard version)
Codeforces
1900
原题
← B1C →
翻译中…

The only difference between the easy and hard versions is that the given string $s$ in the easy version is initially a palindrome, this condition is not always true for the hard version.

翻译中…

A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" is not.

翻译中…

Alice and Bob are playing a game on a string $s$ of length $n$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.

翻译中…

In each turn, the player can perform one of the following operations:

翻译中…
  1. Choose any $i$ ($1 \le i \le n$), where $s[i] =$ '0' and change $s[i]$ to '1'. Pay 1 dollar.
  2. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa.
翻译中…

Reversing a string means reordering its letters from the last to the first. For example, "01001" becomes "10010" after reversing.

翻译中…

The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 10^3$). Then $t$ test cases follow.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$).

翻译中…

The second line of each test case contains the string $s$ of length $n$, consisting of the characters '0' and '1'. It is guaranteed that the string $s$ contains at least one '0'.

翻译中…

Note that there is no limit on the sum of $n$ over test cases.

翻译中…

Output

翻译中…

For each test case print a single word in a new line:

翻译中…
  • "ALICE", if Alice will win the game,
  • "BOB", if Bob will win the game,
  • "DRAW", if the game ends in a draw.
翻译中…

Note

翻译中…

In the first test case of example,

翻译中…
  • in the $1$-st move, Alice will use the $2$-nd operation to reverse the string, since doing the $1$-st operation will result in her loss anyway. This also forces Bob to use the $1$-st operation.
  • in the $2$-nd move, Bob has to perform the $1$-st operation, since the $2$-nd operation cannot be performed twice in a row. All characters of the string are '1', game over.
翻译中…

Alice spends $0$ dollars while Bob spends $1$ dollar. Hence, Alice wins.

翻译中…

In the second test case of example,

翻译中…
  • in the $1$-st move Alice has to perform the $1$-st operation, since the string is currently a palindrome.
  • in the $2$-nd move Bob reverses the string.
  • in the $3$-rd move Alice again has to perform the $1$-st operation. All characters of the string are '1', game over.
翻译中…

Alice spends $2$ dollars while Bob spends $0$ dollars. Hence, Bob wins.

翻译中…

样例

Input #1
3
3
110
2
00
4
1010
Output #1
ALICE
BOB
ALICE
InputCopy OutputCopy
题目信息
题号
1527B2
时间限制
1 s
内存限制
256 MB
标签
构造
博弈论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。