google code jam 2015 C. Dijkstra

Problem C. Dijkstra

Confused? Read the  quick-start guide.
Small input
11 points
You have solved this input set.
Large input
17 points
You have already tried this input set. (Judged at the end of the contest.)

Problem

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 ij, or k. 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 i by j, look in the row for i and the column for j to find that the answer is k. To multiply j by i, look in the row for j and the column for i to find that the answer is -k.

As you can see from the above examples, the quaternions are not commutative -- that is, there are some a and b for which a * b != b * a. However they are associative -- for any ab, and c, it's true that a * (b * c) = (a * b) * c.

Negative signs before quaternions work as they normally do -- for any quaternions a and b, it's true that -a * -b = a * b, and -a * b = a * -b = -(a * b).

You want to argue that your misspelling was equivalent to the correct spelling ijk by showing that you can split your string of is, js, and ks in two places, forming three substrings, such that the leftmost substring reduces (under quaternion multiplication) to i, the middle substring reduces to j, and the right substring reduces to k. (For example, jijwould be interpreted as j * i * jj * i is -k, and -k * j is i, so jij reduces to i.) If this is possible, you will get your point back. Can you find a way to do it?

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line with two space-separated integers L and X, followed by another line with L characters, all of which are ij, or k. 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 L characters repeated X times. For instance, for L = 4, X = 3, and the given string kiij, your input string would be kiijkiijkiij.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is either YES or NO, depending on whether the string can be broken into three parts that reduce to ij, and k, in that order, as described above.

Limits

1 ≤ T ≤ 100.
1 ≤ L ≤ 10000.

Small dataset

1 ≤ X ≤ 10000.
1 ≤ L * X ≤ 10000.

Large dataset

1 ≤ X ≤ 1012.
1 ≤ L * X ≤ 1016.

Sample


Input 
 

Output 
 
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
In Case #1, the string is too short to be split into three substrings.

In Case #2, just split the string into  ij, and  k.

In Case #3, the only way to split the string into three parts is  kji, and this does not satisfy the conditions.

In Case #4, the string is  jijijijijiji. It can be split into  jij (which reduces to  i),  iji(which reduces to  j), and  jijiji (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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值