#P13213. [GCJ 2015 Qualification] Dijkstra
[GCJ 2015 Qualification] Dijkstra
题目描述
The Dutch computer scientist Edsger Dijkstra made many important contributions to the field, including the shortest path finding algorithm that bears his name. This problem is not about that algorithm.
You were marked down one point on an algorithms exam for misspelling "Dijkstra" -- between d and stra, you wrote some number of characters, each of which was either , or . You are prepared to argue to get your point back using quaternions, an actual number system (extended from complex numbers) with the following multiplicative structure:
To multiply one quaternion by another, look at the row for the first quaternion and the column for the second quaternion. For example, to multiply by , look in the row for and the column for to find that the answer is . To multiply by , look in the row for and the column for to find that the answer is .
As you can see from the above examples, the quaternions are not commutative -- that is, there are some and for which $\mathbf{a} \times \mathbf{b} \neq \mathbf{b} \times \mathbf{a}$. However they are associative -- for any , , and , it's true that $\mathbf{a} \times (\mathbf{b} \times \mathbf{c}) = (\mathbf{a} \times \mathbf{b}) \times \mathbf{c}$.
Negative signs before quaternions work as they normally do -- for any quaternions and , it's true that $-\mathbf{a} \times -\mathbf{b} = \mathbf{a} \times \mathbf{b}$, and $-\mathbf{a} \times \mathbf{b} = \mathbf{a} \times -\mathbf{b} = -(\mathbf{a} \times \mathbf{b})$.
You want to argue that your misspelling was equivalent to the correct spelling ijk
by showing that you can split your string of i
s, j
s, and k
s in two places, forming three substrings, such that the leftmost substring reduces (under quaternion multiplication) to , the middle substring reduces to , and the right substring reduces to . (For example, jij
would be interpreted as ; is , and is , so jij
reduces to .) If this is possible, you will get your point back. Can you find a way to do it?
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each consists of one line with two space-separated integers and , followed by another line with characters, all of which are , , or . Note that the string never contains negative signs, 1s, or any other characters. The string that you are to evaluate is the given string of characters repeated times. For instance, for , , and the given string , your input string would be .
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is either or , depending on whether the string can be broken into three parts that reduce to , , and , in that order, as described above.
5
2 1
ik
3 1
ijk
3 1
kji
2 6
ji
1 10000
i
Case #1: NO
Case #2: YES
Case #3: NO
Case #4: YES
Case #5: NO
提示
Sample Explanation
In Case #1, the string is too short to be split into three substrings.
In Case #2, just split the string into i, j, and k.
In Case #3, the only way to split the string into three parts is k, j, i, and this does not satisfy the conditions.
In Case #4, the string is . It can be split into (which reduces to i), (which reduces to j), and (which reduces to k).
In Case #5, no matter how you choose your substrings, none of them can ever reduce to a j or a k.
Sample Explanation
- .
- .
Small dataset(11 Pts)
- Time limit:
2405 seconds. - .
- .
Large dataset(17 Pts)
- Time limit:
48010 seconds. - .
- .