#C2784. Word Puzzle Score

    ID: 46138 Type: Default 1000ms 256MiB

Word Puzzle Score

Word Puzzle Score

You are given a letter grid and a list of words. For each word, check whether it can be formed using the letters in the grid. A word is considered formable if for every letter in the word, the count of that letter in the word is less than or equal to its count in the grid.

The score for each valid word is defined as follows:

  • If the length \( L = 2 \), the score is \( 1 \).
  • If \( L = 3 \), the score is \( 2 \).
  • If \( L \ge 4 \), the score is \( 3 \).
  • Otherwise, the score is \( 0 \).

Your task is to compute the total score for all words that can be formed with the grid.

inputFormat

The input is given from standard input (stdin) with the following format:

R C
row1
row2
... 
rowR
W
word1
word2
...
wordW

Where:

  • R and C are two integers representing the number of rows and columns of the grid.
  • Each of the following R lines contains C uppercase letters separated by spaces.
  • W is an integer denoting the number of words.
  • Each of the next W lines contains a word composed of uppercase letters.

outputFormat

Print a single integer to standard output (stdout) representing the total score of all valid words.

## sample
4 4
A C D E
B A C D
E F G H
I J K L
3
CADE
AFE
XYZ
5