Hello,
I don't quite understand the solution for the USACO Platinum December Contest Problem 1. Can anyone explain it with more detail? Thanks!
Problem: http://usaco.org/index.php?page=viewproblem2&cpid=768
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hello,
I don't quite understand the solution for the USACO Platinum December Contest Problem 1. Can anyone explain it with more detail? Thanks!
Problem: http://usaco.org/index.php?page=viewproblem2&cpid=768
Name |
---|
Let's do an example:
Assume we have cow "aaab" and "abab" then we have the substrings {"a", "b", "aa", "ab", "aaa", "aab", "aaab"} for the first cow and {"a","b","ab","ba","aba","bab","abab"} for the second cow.
Unique substrings are then {"aa","aaa","aab","aaab"} for the first cow and {"ba","aba","bab","abab"} for the second cow.
First we (implicityly) generate the suffixes {"", "b", "ab", "aab", "aaab"} of first cow and {"", "b", "ab", "bab", "abab"} of the second cow and sort them together:
Now for each suffix we count the number unique substrings of that suffix including its first character. Therefore we search for the previous and next suffix of another cow and calculate the length of their longest common prefix. Up to this length the substrings of this suffix are not unique.
For example "abab": the next/previous suffix of another cow is "b"/"ab" and the lengths of their longest common prefixes are 0/2. Therefore only the substrings "aba" and "abab" can be unique substrings.
Additionally one is also not allowed to count multiple occurrences within a word multiple times. Therefore also the length of the longest common prefix of the previous suffix belonging to the same cow is relevant as substrings up to that length are already counted at that suffix.
for example "aab": the next/previous suffix of another cow is "ab"/"" and the lengths of their longest common prefixes are 1/0. Therefore the substrings "aa" and "aab" could be unique substrings but the string "aa" is already counted during the calculation of "aaab". To avoid this double counting we also evaluate the length of the longest common prefix with previous suffix of same cow which is 2 in this case. Therefore only the substring "aab" is a new unique substring.
Here is the list of the length of longest common prefix(lcp) of previous suffix of same cow, previous suffix of other cow and next suffix of other cow. The number of new unique substrings is then the length of the suffix minus the maximal lcp value.
Therefore we have (3+1)=4 unique substrings for the first cow and (2+2)=4 unique strings for second cow, which confirms the sizes of the sets.