#P3883. Chess Motif Finder

    ID: 17131 Type: Default 1000ms 256MiB

Chess Motif Finder

Chess Motif Finder

In the jloi-08 game there are numerous chess motifs (定式) which are common move combinations. When analyzing a game, you are asked: How many motifs are present? And what are they?

A chess game is composed of many moves. Each move is a string such as Kh2 or Nxb7. The first character is one of the six pieces: K, Q, B, N, R, and P. This is optionally followed by an 'x' indicating a capture, and then a coordinate.

A coordinate is defined as a lowercase letter from \(a\) to \(h\) and a digit from \(1\) to \(8\), i.e. it matches the regular expression [a-h][1-8].

A motif is said to appear in the game if all its moves appear consecutively as a contiguous subsequence of the full game's moves.

Your task is to determine, given a complete game and several motif queries, how many and which motifs appear in the game.

inputFormat

The input consists of:

  1. An integer N representing the number of moves in the chess game.
  2. A line containing N moves separated by spaces.
  3. An integer M representing the number of motifs to check.
  4. M lines each containing a motif (a sequence of moves separated by spaces).

outputFormat

The output should contain:

  1. The first line is an integer representing the number of motifs that appear in the game.
  2. Then, for each motif (in the order of input) that appears in the game, output the motif on a new line.

sample

10
Kh2 Qe2 Bb3 Nxb7 Rf1 Pa4 Kh2 Qe2 Bb3 Nxb7
3
Kh2 Qe2 Bb3
Qe2 Bb3 Nxb7
Pa4 Kh2
3

Kh2 Qe2 Bb3 Qe2 Bb3 Nxb7 Pa4 Kh2

</p>