Connection Status:
Competition Arena > StepHopJumpMedium
SRM 814 · 2021-09-28 · by misof · Dynamic Programming
Class Name: StepHopJumpMedium
Return Type: int
Method Name: countWays
Arg Types: (string)
Problem Statement

Problem Statement

You are traversing a dangerous section of a video game level. The section contains some solid terrain blocks (denoted '-') and some blocks with spiky traps (denoted '*'). If you step onto a trap, you die.

You are given the layout of the level in the String level. You are currently standing on the leftmost block of the level. This block is represented by the character level[0] and it is guaranteed to be a solid block.

Your task is to reach the block at the opposite end of the level. This block is also guaranteed to be solid.

You can move in three ways:

  • Take a step (denoted 'S'): move one block to the right.
  • Make a hop (denoted 'H'): hop over one block and land two block to the right of your current block.
  • Make a jump (denoted 'J'): jump over two consecutive blocks and land three blocks to the right of your current position.

For example, if the level is "----", you can traverse it by making three steps (denoted "SSS"), a hop followed by a step ("SH"), a step followed by a hop ("HS"), or a single jump ("J").

If the level is "-**-", the only way to traverse it is by making a jump - remember that you cannot step onto the spiky traps.


In the above way, we can describe any way of traversing the level as a string in which each character is 'S', 'H', or 'J'. Two ways of traversing a level are considered different if they are described by different strings. For example, there are four ways to traverse the level "----", one way to traverse the level "-**-**-", and no ways at all to traverse the level "-*-****-*-".

Let W be the number of different ways in which we can traverse the level described by the String level. Calculate and return the value (W modulo 1,000,000,007).

Constraints

  • level will contain between 2 and 200 characters, inclusive.
  • Each character of level will be either '-' or '*'.
  • The first and the last character of level will be '-'.
Examples
0)
"----"
Returns: 4

The first example from the problem statement.

1)
"-**-"
Returns: 1

The second example from the problem statement.

2)
"-*-****-*-"
Returns: 0

The third example from the problem statement: there are no solutions.

3)
"------------------------------------"
Returns: 132436845

This level consists of 36 dashes. It can be traversed in many different ways. One of them is taking 35 steps, another is taking 11 jumps and then a hop. There are exactly 1,132,436,852 different ways in total. Remember that you should return this value modulo 10^9 + 7.

4)
"--"
Returns: 1

Submissions are judged against all 101 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class StepHopJumpMedium with a public method int countWays(string level) · 101 test cases · 2 s / 256 MB per case

Submitting as anonymous