Rock Skipping

Interview

Time limit1sMemory limit128 MB

Summary
For each lake map, find the throw (start, skip distance) that maximizes count, then length, then start, then smallest distance, and print it.
Level

Medium5 of 10

Topics
Brute force, Implementation, Simulation, Array
Solved
No attempts yet

Problem

As a member of the International Rock-Skipping League, you travel to a different lake each week and compete in a rock-skipping contest. The goal is to throw a rock so that it skips on the water as many times as possible; the exact rules for determining the winner are given below. To make the competitions interesting, the League often chooses lakes with logs, sandbars, and other obstacles.

A lake is given as a side-view, water-level "map", like the first line of the example below (the numbers 0..29 beneath the map are just for reference). A period . indicates clear water, where a rock will skip; any other character indicates an obstacle that will stop a rock.

...=...**..#...@....:.:.:..=..
______________________________
          11111111112222222222
012345678901234567890123456789

You stand at the left end of the lake. You can throw a rock so that it first lands at any position in the lake and then skips at a fixed interval thereafter. So a throw is a pair (i, d), where i (i ≥ 0) is the initial landing position and d (d > 0) is the distance between skips. Note that d must be positive.

The count of a throw is the number of times it skips on the water. The length of a throw is the position of its last contact with either water or an obstacle. To rank two distinct throws, apply the following criteria in order until a winner is determined:

  1. count: the higher value wins.
  2. length: the greater value wins.
  3. initial position i: the greater value wins.
  4. distance between skips d: the smaller value wins.

For the map above, throw (27, 2) hits the obstacle at position 27; it has count 0 and length 27. Throw (16, 1) skips at positions 16, 17, 18, and 19, then hits the obstacle at position 20; it has count 4 and length 20, so it beats (27, 2). Throw (2, 7) skips at positions 2, 9, 16, and 23, then skips over the lake; it has count 4 and length 23, so it beats (16, 1). Throw (1, 4) skips at positions 1, 5, 9, 13, 17, 21, 25, and 29, then skips over the lake; it has count 8 and length 29, and is the best possible throw for this lake.

Input

The input consists of one or more lake maps, each on a line by itself and 1 to 40 characters long, followed by a line containing only END that marks the end of the input. Positions within a map are numbered starting from zero. A map contains only printable ASCII punctuation characters. A period . indicates clear water and any other character indicates an obstacle.

Output

For each map, compute the best possible throw (i, d) and print a line containing i and d separated by a single space.

Examples1

  1. Example 1

    Input
    ...=...**..#...@....:.:.:..=..
    .(+)
    /^\.
    *++&*
    END
    
    Expected output
    1 4
    0 3
    3 1
    4 1