#P3318. [SDOI2015] 双旋转字符串

[SDOI2015] 双旋转字符串

Description

Given two sets of strings S\mathcal{S} and T\mathcal{T}. All strings in S\mathcal{S} have length exactly NN, and all strings in T\mathcal{T} have length exactly MM. Moreover, N+MN + M is even.

If we denote all strings in S\mathcal{S} by S1,S2,,STotalSS_1, S_2, \dots, S_{Total_S}, and all strings in T\mathcal{T} by T1,T2,,TTotalTT_1, T_2, \dots, T_{Total_T}, we want to know how many pairs i,j\langle i, j \rangle satisfy that the concatenation Si+TjS_i + T_j has the double rotation property.

An even-length string WW can be written as the concatenation of two strings of equal length, i.e., W=U+VW = U + V. If VV can be obtained from UU by a rotation (cyclic shift), then WW is said to have the double rotation property. For example, the string vijos can be rotated to ijosv, josvi, osvij, or svijo. Thus, vijosjosvi has the double rotation property.

Input Format

The first line contains four positive integers TotalS,TotalT,N,MTotal_S, Total_T, N, M, denoting respectively the size of set S\mathcal{S}, the size of set T\mathcal{T}, the length of strings in S\mathcal{S}, and the length of strings in T\mathcal{T}.

The next TotalSTotal_S lines each contain a string SiS_i, 1iTotalS1 \le i \le Total_S. Each string has length exactly NN, and consists only of the 26 lowercase letters.

The next TotalTTotal_T lines each contain a string TiT_i, 1iTotalT1 \le i \le Total_T. Each string has length exactly MM, and consists only of the 26 lowercase letters.

Output Format

Output a single integer, the number of pairs i,j\langle i, j \rangle that satisfy the requirement.

4 4 7 3
vijosvi
josvivi
vijosos
ijosvsv
jos
vij
ijo
jos
6

Hint

For 10%10\% of the testdata, 1N1001 \leq N \leq 100, 1M1001 \leq M \leq 100, 1TotalS1001 \leq Total_S \leq 100, 1TotalT1001 \leq Total_T \leq 100.

For 30%30\% of the testdata, 1N5001 \leq N \leq 500, 1M5001 \leq M \leq 500, 1TotalS5001 \leq Total_S \leq 500, 1TotalT5001 \leq Total_T \leq 500.

For 60%60\% of the testdata, $2 \le N \times Total_S + M \times Total_T \le 4 \times 10^5$.

For 100%100\% of the testdata, $2 \le N \times Total_S + M \times Total_T \le 4 \times 10^6$.

Translated by ChatGPT 5