#P3318. Double Rotation Concatenation

    ID: 16575 Type: Default 1000ms 256MiB

Double Rotation Concatenation

Double Rotation Concatenation

You are given two sets of strings S and T. Every string in S has a fixed length \(N\) and every string in T has a fixed length \(M\). It is guaranteed that \(N+M\) is even.

Let the strings in S be \(S_1, S_2, \dots, S_{|S|}\) and the strings in T be \(T_1, T_2, \dots, T_{|T|}\). For each pair \(\langle i,j \rangle\), consider the concatenation \(W = S_i + T_j\). Since \(|W| = N+M\) is even, we can split \(W\) into two halves of equal length: \(W = U+V\), where \(U\) is the prefix and \(V\) is the suffix, each of length \(L = \frac{N+M}{2}\).

The string \(W\) is said to have the double rotation property if \(V\) can be obtained by a non-trivial rotation of \(U\). In other words, there exists an integer \(r\) with \(1 \le r < L\) such that:

[ V = U[r:] + U[:r] ]

For example, if \(U = \texttt{vijos}\), its rotations (excluding the 0 rotation) are \(\texttt{ijosv}\), \(\texttt{josvi}\), \(\texttt{osvij}\) and \(\texttt{svijo}\). Thus, the concatenation \(\texttt{vijos}+\texttt{josvi} = \texttt{vijosjosvi}\) satisfies the double rotation property.

Your task is to count the number of pairs \(\langle i,j \rangle\) such that the concatenated string \(S_i+T_j\) satisfies the double rotation property.

inputFormat

The input begins with two space-separated integers, \(s\) and \(t\), representing the number of strings in sets S and T respectively.

Then follow \(s\) lines each containing a string of length \(N\) (all strings in S have the same length).

After that, there are \(t\) lines each containing a string of length \(M\) (all strings in T have the same length). It is guaranteed that \(N+M\) is even.

outputFormat

Output a single integer representing the number of pairs \(\langle i,j \rangle\) such that the concatenated string \(S_i+T_j\) satisfies the double rotation property.

sample

2 2
ab
xy
ba
yx
2