#P16659. [GKS 2018 #H] Big Buttons

    ID: 19022 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>字符串2018组合数学Google Kick Start

[GKS 2018 #H] Big Buttons

Problem Description

You are a contestant on a popular new game show and are playing for the grand prize!

There are two big buttons, a red one and a black one. You will make a sequence of exactly NN button presses.

There are lots of different sequences of presses you could make, but there are PP forbidden prefixes, each of length no greater than NN. If you make a sequence of presses which begins with any of the forbidden sequences, you will not win the grand prize. It is fine for your sequence to contain one or more forbidden prefixes as long as they do not appear at the start of your sequence.

A winning sequence must consist of exactly NN button presses and must not begin with one of the forbidden prefixes. How many different winning sequences are there?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers NN and PP, as described above. Then, there are PP more lines, each of which contains a string of between 11 and NN characters, inclusive, describing one of the forbidden sequences of presses. An R represents pressing the red button, whereas a B represents pressing the black button.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 11) and y is the number of winning sequences, as described above.

3
3 2
BBB
RB
5 1
R
4 3
R
B
RBRB
Case #1: 5
Case #2: 16
Case #3: 0
1
50 5
BRBRBBBRBRRRBBB
BRBRBRRRBRRRBRB
BBBRBBBRBRRRBBB
BRBRBRRRBRRRB
BRBRBBBRBBBRB
Case #1: 1125556309458944

Hint

In the first case, you must make a sequence of 33 presses. There are 88 possible sequences of three presses, but some of them will cause you to lose the game. They are listed below:

  • RBB. This is forbidden since it starts with the first forbidden sequence (RB).
  • RBR. This is forbidden since it starts with the first forbidden sequence (RB).
  • BBB. This is forbidden since it starts with the second forbidden sequence (BBB).

Thus, there are only 55 winning sequences.

In the second case, you must make a sequence of 55 presses. There is only one forbidden sequence, which is R. This means that the first press must be B, and the next 44 presses can be either button. This gives a total of 1616 different button presses.

In the third case, you must make a sequence of 44 presses. There are three forbidden sequences, but since every possible sequence begins with either R (the first forbidden sequence) or B (the second forbidden sequence), there are no winning sequences. So the answer is 00.

Limits

1T1001 \le T \le 100.

1Pmin(2N,100)1 \le P \le \min(2^N, 100).

Each forbidden prefix is between 11 and NN characters long, inclusive.

No two forbidden prefixes will be the same.

Small dataset (Test set 1 - Visible)

1N101 \le N \le 10.

Large dataset (Test set 2 - Hidden)

1N501 \le N \le 50.