Super-difficult problem
Limits: 4 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Bogdan was given a super-difficult task — to write a problem statement for the international collegiate programming contest. Such an honorable task was entrusted to him due to his remarkable skills in writing problem statements. These skills are the result of daily work on statements.
Bogdan knows that in order to write good statements, one only needs to slightly modify last year’s ones. Last year he already wrote two such statements. These statements can be written as two strings — \(s\) and \(t\). Bogdan is sure that in order to write a good statement, one needs to know how many identical pairs of substrings there are in strings \(s\) and \(t\). Of course he could find this himself, but he has no time — the contest is approaching fast. Help him find how many identical pairs of substrings there are in strings \(s\) and \(t\). That is, you need to find the number of quadruples \(l_1\), \(r_1\), \(l_2\), \(r_2\), such that \(r_1 - l_1 = r_2 - l_2\) and \(s[l_1 ... r_1] = t[l_2 ... r_2]\).
A substring is a non-empty contiguous part of a string. That is, a string that can be obtained by deleting any number of characters from the beginning and the end of the string.
Input
The first line gives a single string \(s\).
The second line gives a single string \(t\).
Output
In a single line, output the number of identical pairs of substrings of strings \(s\) and \(t\).
Constraints
\(1 \le |s| \le 10^4\),
\(1 \le |t| \le 300\),
\(|s|\) — denotes the length of the string,
the strings consist only of lowercase English letters
(a-z).
Problem evaluation consists of the following subtasks:
1 point — example from the statement,
4 points — a block of tests in which \(|s| \le 50\) and \(|t| \le 50\),
10 points — a block of tests in which \(|s| \le 300\) and \(|t| \le 300\),
10 points — a block of tests in which \(|s| \le 10^4\) and \(|t| \le 300\),
You will receive the points for a subtask only if you give the correct answer to all tests in the subtask.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| aba aab | 6 |
Notes
In the first example, there is one common pair of substrings ab, one pair of substrings b, and 4 pairs of substrings a.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Super-difficult problem - Editorial
Go over all substrings of \(s\). For each of them, go over all substrings of \(t\) and compare them for equality. This solution works in \(O(|s|^2 \cdot |t|^2 \cdot cmp)\), where cmp is the time to compare two substrings, which is \(O(|t|)\) in the worst case. It passes the first test group.
Consider a faster solution. Go over all substrings of \(t\) and put them into a multiset. Now for each substring of \(s\), instead of going over the substrings of \(t\), we just look up how many such substrings our multiset has. This solution works in \(O((|t|^2 + |s|^2) \cdot log(|t|^2) \cdot cmp)\), where cmp is the time to compare two substrings, which is \(O(|t|)\) in the worst case. It passes the first two test groups.
Now consider a solution for the full constraints. Suppose we found substrings of \(s\) and \(t\) that coincide. Clearly, all their corresponding substrings coincide as well. If the length of the matching substrings is \(n\), we can add \(\frac{n \cdot (n + 1)}{2}\) to the answer and not consider their substrings anymore. Then if we align \(s\) and \(t\) in all possible positions and find the ranges where the strings coincide, we can easily compute the answer.
For example, if \(s\) = aabcabb and \(t\) = abba, one of the alignments looks like this:
Here there are substrings of lengths 2 and 1, so we add \(\frac{2 \cdot 3}{2} + \frac{1 \cdot 2}{2} = 3 + 1 = 4\) to the answer. Note that the string \(t\) can go beyond \(s\), for example like this:
Since there are \(|s| + |t| - 1\) positions to align them, the total complexity of this algorithm is \(O((|s| + |t|) \cdot |t|)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|