TopcoderArchiveVisit Topcoder
SRM25 Jun 2013
SRM 591

StringPath

Problem statement, definition, constraints, and public examples.

Problem Statement

Manao has a rectangular board divided into n times m cells. The rows of the board are numbered from 1 to n and the columns are numbered from 1 to m. The cell in row i, column j is referred to as (i, j). Each cell contains an uppercase letter of the English alphabet.

Having such a board, Manao likes to traverse it. The traversal always starts in the cell (1, 1). In each step of the traversal Manao moves either one cell down or one cell to the right. That is, from any cell (x, y) Manao will move either to (x+1, y) or to (x, y+1). The traversal always ends in the cell (n, m). During the traversal Manao records the letters in the visited cells (including the first and the last cell). The obtained string is called a string path for the given board.

You are given the ints n and m, and two distinct Strings A and B. Manao claims that he performed two different traversals of his n x m board and obtained the string paths A and B. Compute the number of different boards for which this is possible. Return this number modulo 1,000,000,009.

Definition

Class:
StringPath
Method:
countBoards
Parameters:
int, int, String, String
Returns:
int
Method signature:
int countBoards(int n, int m, String A, String B)
(be sure your method is public)

Constraints

  • n will be between 1 and 8, inclusive.
  • m will be between 1 and 8, inclusive.
  • A and B will be exactly n+m-1 characters long.
  • A and B will consist of uppercase letters ('A'-'Z') only.
  • A and B will be distinct.

Examples

  1. 2
    2
    "ABC"
    "ADC"
    Returns: 2
    The two possible boards are: AB AD DC BC
  2. 2
    2
    "ABC"
    "ABD"
    Returns: 0
    It is impossible for two string paths to have a different last letter.
  3. 3
    4
    "ABCDDE"
    "ACCBDE"
    Returns: 1899302
  4. 8
    8
    "ZZZZZZZZZZZZZZZ"
    "ZABCDEFGHIJKLMZ"
    Returns: 120390576
Back to all problems