Under construction

Still being built. Things may change

Oversized Pancake Flipper (Large)

Time limit5sMemory limit512 MB

Summary
Given a row of happy or blank pancakes and a flipper of fixed width K, find the minimum number of flips that makes every pancake happy, or report that it is impossible.
Level

Medium5 of 10

Topics
Greedy, Sliding window
Solved
No attempts yet

Problem

A pancake house introduced a new kind of pancake. One side has a happy face made of chocolate chips (the happy side), and the other side has nothing on it (the blank side).

You are the head cook on duty. The pancakes cook in a single row on a hot surface. To work faster, the house has bought an oversized pancake flipper that flips exactly KK consecutive pancakes at once. Inside that range of KK pancakes it turns every happy side up pancake blank side up and every blank side up pancake happy side up. It does not change the left to right order of the pancakes.

Raised borders run along both sides of the cooking surface, so you cannot flip fewer than KK pancakes at a time, not even at the ends of the row. For example, you can flip the leftmost KK pancakes, but not the leftmost K−1K - 1 pancakes.

Your apprentice cook, who is still learning the job, used the old single pancake flipper to turn over some individual pancakes and then walked off with it. Customers are about to visit the kitchen. The oversized flipper is the only tool left, and every pancake has to end up happy side up.

Given the current state of the pancakes, compute the minimum number of uses of the oversized pancake flipper that leaves every pancake happy side up, or report that no sequence of uses does it.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains a string SS and an integer KK, separated by a space. SS describes the row of pancakes. Each of its characters is either +, a pancake that starts happy side up, or -, a pancake that starts blank side up.

Limits

  • 1≤T≤1001 \le T \le 100
  • Every character of SS is either + or -.
  • 2≤K≤∣S∣2 \le K \le |S|
  • 2≤∣S∣≤10002 \le |S| \le 1000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1. yy is IMPOSSIBLE when no sequence of flips leaves every pancake happy side up, and otherwise the minimum number of times the oversized flipper has to be used.

Notes

When SS is ---+-++- and K=3K = 3, flip the leftmost 3 pancakes to reach ++++-++-, then the rightmost 3 to reach ++++---+, then the 3 pancakes that are still blank side up. Other sequences also finish in 3 flips, and none finishes in 2 or fewer.

When SS is +++++ and K=4K = 4, every pancake is already happy side up, so no flip is needed.

When SS is -+-+- and K=4K = 4, every legal flip covers both the second and the third pancake from the left, so those two always turn together. They start on opposite sides, so they can never show the same side, and no sequence of flips leaves the whole row happy side up.

Examples5

  1. Example 1

    Input
    3
    ---+-++- 3
    +++++ 4
    -+-+- 4
    
    Expected output
    Case #1: 3
    Case #2: 0
    Case #3: IMPOSSIBLE
    
  2. Example 2

    Input
    4
    -- 2
    +- 2
    -+ 2
    ++ 2
    
    Expected output
    Case #1: 1
    Case #2: IMPOSSIBLE
    Case #3: IMPOSSIBLE
    Case #4: 0
    
  3. Example 3

    Input
    5
    ----- 5
    +++++ 5
    ----+ 5
    +---- 5
    -++++ 5
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: IMPOSSIBLE
    Case #4: IMPOSSIBLE
    Case #5: IMPOSSIBLE
    
  4. Example 4

    Input
    4
    -+-+-+-+ 2
    +-+-+-+- 2
    -------- 2
    ++-- 2
    
    Expected output
    Case #1: 4
    Case #2: 4
    Case #3: 4
    Case #4: 1
    
  5. Example 5

    Input
    6
    -+- 2
    --+ 2
    +-- 2
    -++ 3
    ++- 3
    -+- 3
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 1
    Case #4: IMPOSSIBLE
    Case #5: IMPOSSIBLE
    Case #6: IMPOSSIBLE