#K41332. Distinct Substrings and Swap Optimization
Distinct Substrings and Swap Optimization
Distinct Substrings and Swap Optimization
You are given a string s of length n. Your task is to perform two computations:
- First, count the number of distinct non-empty substrings of s. A substring is any contiguous segment of characters.
- Second, determine the maximum number of distinct substrings that can be obtained by performing at most one swap of any two characters in s.
Formally, for a string s, the number of distinct substrings is given by \[ \text{count}(s) = |\{ s[i:j] \mid 0 \le i < j \le |s| \}|, \] where \( s[i:j] \) denotes the substring of s from index i (inclusive) to j (exclusive). The swap operation allows you to exchange the characters at two positions in s, and you are allowed to perform at most one such swap.
Output the answer for the original string and then the answer for the best possible string after at most one swap.
inputFormat
The input is read from standard input and has the following format:
n s
Where:
n
is an integer representing the length of the string.s
is a string of lengthn
consisting of lowercase letters.
outputFormat
Print two integers on separate lines:
- The number of distinct substrings of
s
. - The maximum number of distinct substrings that can be obtained by performing at most one swap on
s
.
The output should be written to standard output.
## sample5
abcde
15
15
</p>