Dijkstra (Large)
Time limit5sMemory limit512 MB
Given a short string of i, j and k repeated X times, decide whether it splits into three nonempty parts that multiply to i, j and k under quaternion rules.
- Level
Medium7 of 10
- Topics
- Math, Simulation
- Solved
- No attempts yet
Problem
A shortest path algorithm carries the name of the Dutch computer scientist Edsger Dijkstra. This problem is not about that algorithm.
You lost one point on an algorithms exam for misspelling "Dijkstra": between D and stra you wrote some number of characters, each of them i, j, or k. To get the point back you appeal to the quaternions, a number system extended from the complex numbers whose multiplication follows this table.
To multiply one quaternion by another, read the cell where the row of the first factor meets the column of the second factor. For example, is , and is .
Quaternion multiplication is not commutative: there are and with . It is associative: for all , , we have .
Signs behave as usual. For all and , , and .
You want to show that what you wrote is equivalent to the correct spelling ijk. Cut the string at two positions into three non-empty pieces so that, under quaternion multiplication, the first piece reduces to , the middle piece reduces to , and the last piece reduces to . For example, jij is read as : is , and is , so jij reduces to . Decide whether such a cut exists.
Input
The first line has the number of test cases . Then test cases follow, each on two lines. The first line has two space separated integers and . The second line has a string of characters, each of them i, j, or k. The string never contains a sign, a 1, or any other character. The string you evaluate is that string of characters repeated times. For , and the given string kiij, the string you evaluate is kiijkiijkiij.
Limits
Output
For each test case print one line Case #x: y, where is the test case number starting from 1, and is YES if the string can be cut into three pieces reducing to , , and in that order, and NO otherwise.
Sample explanation
The five cases of the first sample input work out as follows.
In case 1 the string is too short to be split into three pieces.
In case 2, split the string into i, j, and k.
In case 3 the only split into three pieces is k, j, i, and that split does not meet the condition.
In case 4 the string is jijijijijiji. It splits into jij, which reduces to , then iji, which reduces to , then jijiji, which reduces to .
In case 5 no cut ever produces a piece that reduces to or to .