#P4076. [SDOI2016] 墙上的句子
[SDOI2016] 墙上的句子
Description
Archaeologists found a white wall written in an unknown language. On it there is an -by- grid. Some cells contain an uppercase letter from A to Z, and some cells are blank.
A contiguous run of letters strictly horizontally or vertically forms a word. Each row may be read either left to right or right to left, and each column may be read either bottom to top or top to bottom. That is, for each row, reading from left to right can be regarded as a sentence formed by several words, where adjacent words are separated by one or more blank cells; or it may be a sentence read from right to left. The vertical direction is analogous.
Unfortunately, we do not completely know the reading order of each row and each column. However, it is plausible that some words satisfy that their reversed form is also a word. For example, the word BOY and its reversal YOB are both English words.
Furthermore, observers found that for each row (column), under its finalized reading order, all words read in that row (column) simultaneously satisfy either “their own lexicographic order is not less than that of their reversed form,” or simultaneously satisfy “their own lexicographic order is not greater than that of their reversed form.”
After determining the reading order of all rows and columns, we can construct a dictionary for this unknown language.
What is the minimum possible number of distinct words in the dictionary whose reversed form is also a word? Please note that if a word and its reversed form are two different words, they must be counted separately; a word that is itself a palindrome should not be counted twice.
Input Format
The first line contains an integer , the number of test cases.
For each test case:
- The first line contains two integers .
- The second line contains numbers, corresponding to the rows. If the -th number is
1, the -th row is read left to right; if it is-1, it is read right to left; if it is0, the direction is undetermined. - The third line contains numbers, corresponding to the columns. If the -th number is
1, the -th column is read top to bottom; if it is-1, it is read bottom to top; if it is0, the direction is undetermined. - Then follow lines. Each line is a string of length , consisting of uppercase letters
AtoZand underscores. An underscore denotes an empty cell.
Output Format
Output lines. For each test case, output a single integer: the minimum number of words such that their reversed form is also a word.
Note that if a word is a palindrome, it certainly satisfies “its reversal is also a word.”
1
2 10
0 0
0 0 0 0 0 0 0 0 0 0
ADA_JARVIS
ADA_SIVRAJ
3
Hint
Constraints: For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号