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.
        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 , 
, and 
 represent a character, the frequency of 
 in 
, and the frequency of 
 in 
 respectively. This can be preprocessed with a frequency array or map.
The number of string crossings is the sum of  for all 
.
We can then test all pairs of characters, as there are only  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 -bit integers were required to pass.
Time Complexity: 
Comments