#P10992. Similar Substring Pairs

    ID: 13041 Type: Default 1000ms 256MiB

Similar Substring Pairs

Similar Substring Pairs

For two strings \(A\) and \(B\) of equal length, if for any indices \(i, j\), the conditions \(A_i = A_j\) and \(B_i = B_j\) are either both true or both false, then we say that \(A\) and \(B\) form a pair of similar strings. For example, aabab and xxkxk are similar strings as their pattern of repeated characters is identical, but abcde and abcdd are not.

Given two strings \(S\) and \(T\), find the largest integer \(k\) such that there exist two substrings \(S'\) of \(S\) and \(T'\) of \(T\) (both of length \(k\)) that are similar. In other words, you need to choose a positive integer \(k\) and find substrings \(S' \subseteq S\) and \(T' \subseteq T\) of length \(k\) with the property that for any indices \(i,j\) in \([1,k]\),

[ (S'_i = S'_j) \iff (T'_i = T'_j), ]

and maximize \(k\). If no positive \(k\) satisfies this property, output \(0\).

inputFormat

The input consists of two lines. The first line contains the string \(S\) and the second line contains the string \(T\). Both strings consist of printable characters and their lengths are at most \(10^5\) (though your solution may assume smaller sizes for the purpose of implementation). It is guaranteed that both strings have length at least 1.

outputFormat

Output a single integer \(k\) --- the maximum length for which there exist substrings \(S' \subseteq S\) and \(T' \subseteq T\) (both of length \(k\)) that form a pair of similar strings.

sample

aabab
xxkxk
5