Editorial for Mock CCC '22 Contest 1 J3 - String Crossing Maximization


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The first subtask rewarded brute force solutions that ran in cubic time.

The second subtask was meant to guide contestants to the full solution.

To solve the problem fully, we realize that only the frequency of each character is relevant.

Let c, f(c), and g(c) represent a character, the frequency of c in S, and the frequency of c in T respectively. This can be preprocessed with a frequency array or map.

The number of string crossings is the sum of f(c) \times g(c) for all c.

We can then test all pairs of characters, as there are only 26 \times 26 = 676 possible combinations. This is done by incrementing/decrementing the frequencies and performing the same string crossing calculation. Other, slightly faster methods do exist.

Note that 64-bit integers were required to pass.

Time Complexity: \mathcal{O}(|S| + |T|)


Comments

There are no comments at the moment.