#P15198. [SWERC 2018] Crosswords

[SWERC 2018] Crosswords

题目描述

In order to attract more tourists in Paris, Anne wants to organize a huge sound and light show at night on the facades of the Louvre palace. The facades are showing windows on several levels, and the windows are also vertically aligned. So Anne has the idea of displaying giant letters through the windows, one per window, so that it forms words both horizontally and vertically.

On a given facade, windows are organized on a rectangular grid with NN rows and MM columns. Anne has gathered a list of AA of NN-letter words and a list of size BB of MM-letter words. She is wondering how many ways there are to display simultaneously NN words of length MM horizontally and MM words of length NN vertically on that grid.

输入格式

The input consists of the following:

  • The first line contains two integers NN and AA separated with a single space.
  • The second line contains two integers MM and BB separated with a single space.
  • The next AA lines contains NN-letter words, one per line.
  • The next BB lines contains MM-letter words, one per line.

输出格式

The output should consist of a single line, whose content is an integer, the total number of distinct word grids.

3 4
4 5
war
are
yes
sat
says
area
test
ways
rest
2
3 7
3 7
ran
age
now
its
the
set
ago
ran
age
now
its
the
set
ago
2

提示

Sample Explanation 1

The solutions are:

s a y s
a r e a
t e s t
w a y s
a r e a
r e s t

Sample Explanation 2

The solutions are:

i t s
t h e
s e t
r a n
a g o
n o w

Limits

The input satisfies 2N,M42 \leq N, M \leq 4 and 1A×B10080161 \leq A \times B \leq 1008016.

Words are taken from the English dictionary. Each of the two lists contains no repetition. Words consist of lowercase letters between 'a' (ASCII code 97) and 'z' (ASCII code 122).

Words from the first list will be displayed vertically, from top to bottom. Words from the second list will be displayed horizontally, from left to right. The same word can be used several times to build a grid, i.e., in several columns (resp., rows) if it belongs to the first (resp., second) list of words.

When N=MN = M, it is not allowed to use words from the first list horizontally (unless they appear in the second list as well), or words from the second list vertically (unless they appear in the first list as well).