Silly substitutions solution kickstart

Silly substitutions solution kickstart

You are given a string SS of length NN which consists of digits 0-9. You do the following operations on the string in the order given.

      1. Find all the substrings 01 and replace each of them with 2.
      2. Find all the substrings 12 and replace each of them with 3.
      3. Find all the substrings 23 and replace each of them with 4.
      4. Find all the substrings 34 and replace each of them with 5.

    .

Silly substitutions solution kickstart

    1. .

 

    1. .

 

  1. Find all the substrings 89 and replace each of them with 0.
  2. Find all the substrings 90 and replace each of them with 1.

You repeat this process in the same given order until none of the above operations change the string. For example, if SS is 12 then we do not stop at operation 11 since it does not affect the string but perform operation 22 and change the string to 3. We can see that the string does not change further no matter how many times we repeat the above process.

Your task is to find how the final string will look like for the given SS.

Input

The first line of the input gives the number of test cases, TTTT test cases follow. Each test case consists of two lines.

The first line of each test case contains an integer NN, denoting the length of the string SS.

The second line of each test case contains a string SS of length NN.

Output Silly substitutions solution kickstart

For each test case, output one line containing Case #xxyy, where xx is the test case number (starting from 1) and yy is the final string obtained.

Limits

Memory limit: 1 GB.
1T1001≤T≤100.
The input string only consists of digits 0-9.

Test Set 1

Time limit: 20 seconds.
1N1001≤N≤100.

Test Set 2

Time limit: 60 seconds.
For at most 10 cases:
1N5×1051≤N≤5×105.
For the remaining cases:
1N1001≤N≤100.

Sample Silly substitutions solution kickstart

Sample Input
content_copy
4
3
012
4
0145
5
00000
11
98765432101
Sample Output
content_copy
Case #1: 22
Case #2: 26
Case #3: 00000
Case #4: 1

In Sample Case #1, substring 01 is replaced with 2 and the resulting string is 22 which is not affected further by any of the operations. Therefore final string is 22.

In Sample Case #2, substring 01 is replaced with 2 and the resulting string is 245. The substring 45 is replaced with 6 and the resulting string is 26 which is not further affected by any of the operations. Therefore final string is 26.

In Sample Case #3, since the operations cannot be performed on the given string, the string does not change.

In Sample Case #4, all the operations can be performed sequentially on the string and the final string is 1.

Leave a Comment