#P8793. Maximizing 'owo' Occurrences
Maximizing 'owo' Occurrences
Maximizing 'owo' Occurrences
Little Blue loves owo. He has a collection of strings and wants to concatenate them in an order that maximizes the number of occurrences of owo in the final string. Occurrences are counted with overlaps allowed; for example, owowo contains $2$ occurrences and owowowo contains $3$ occurrences.
Formally, given strings $S_1, S_2, \dots, S_n$, you may rearrange them arbitrarily. The total count is the sum of:
- The number of times
owoappears within each individual string. - Additional occurrences formed at the junction between consecutive strings. For two consecutive strings $A$ and $B$, an occurrence is formed if either:
- The last two characters of $A$ are
owand the first character of $B$ iso, or - The last character of $A$ is
oand the first two characters of $B$ arewo.
Calculate the maximum possible number of occurrences of owo that can be achieved by a suitable reordering of the strings.
Note: All formulas are in LaTeX format (e.g. $2$, $3$).
inputFormat
The first line contains an integer $n$ ($1 \le n \le 10^5$), representing the number of strings.
Each of the next $n$ lines contains a non-empty string (only lowercase English letters) that you can use to form the final string.
outputFormat
Output a single integer: the maximum number of occurrences of owo in the concatenated string when the strings are arranged optimally.
sample
3
owo
ow
o2