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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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.