The Fibonacci Game
Time limit1sMemory limit128 MB
Determine whether the first player wins a game that erases Fibonacci words only from the right end of a given a/b string.
- Level
Hard8 of 10
- Topics
- String matching, Dynamic programming, Game theory, String
- Solved
- No attempts yet
Problem
Define the Fibonacci words as follows: , , and for , . In other words, the Fibonacci word with index is obtained by concatenating the words with indices and , in that order. For example, , , and .
The Fibonacci game is played by two players on a single word made only of the letters a and b (this word is called the board). The players alternate moves; a single move consists of erasing any one Fibonacci word from the right end of the board. A player who cannot make a move loses. For a given word, determine whether the player who moves first can always win, assuming both players play optimally.
Write a program that reads the number of test cases and, for each one, reads the board on which the game is played from standard input, determines whether the first player can always win, and prints the answer to standard output.
Input
The first line contains one integer (), the number of test cases. Each of the next lines describes one test case. Each line contains a positive integer (), followed by a single space and then a string of the letters a and b of length (with no spaces between the letters). This string is the board on which the game is played.
Output
Print lines. Each line contains the answer for one test case, in the same order as the input. Print TAK if the first player can always win, and NIE otherwise.