problems / 207

Alice and Bob's Palindrome Game

medium games dp palindromes 2800 ms · 256 MB

Alice and Bob's Palindrome Game

Alice and Bob play a game on a string. They take turns removing exactly one character from either the left or right end of the string, starting with Alice. A player wins immediately if, after their move, the remaining string is a palindrome. Determine the winner if both play optimally.

Input

The first line contains an integer n (1 ≤ n ≤ 2000), the length of the string.

The second line contains a string s of length n consisting of lowercase English letters.

Output

Print Alice if Alice wins, or Bob if Bob wins.

Notes

A string is a palindrome if it reads the same forwards and backwards. An empty string is considered a palindrome.

In the first example, s = "abc". Alice can remove either end. If she removes 'a' to get "bc" (not a palindrome), Bob then plays on "bc". If she removes 'c' to get "ab" (not a palindrome), Bob plays on "ab". With optimal play from both, Bob can force a win.

In the second example, s = "aa". Alice can remove either 'a' to get "a", which is a palindrome. So Alice wins immediately.

Example 1

Input

3
abc

Output

Bob

Example 2

Input

2
aa

Output

Alice

Hints · reveal one at a time if you're stuck

Hint 1

At any moment the remaining string is a contiguous piece s[l..r] of the original. How many different positions like this can the game reach, and which of them can be checked in order?

Hint 2

Decide whether each position is a win for the player to move, working from shorter pieces to longer ones. The player to move wins at once if one removal leaves a palindrome, and otherwise wins if some removal leaves the opponent in a losing position. Make the palindrome checks constant time by precomputing them for every piece.

report an issue Prefer your own editor? sooks prep 207 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.