LightOj 1428 (Melody Comparison)
#lightoj #cp #problem_solving #string #suffix_array #kmp #binary_search
Idea
- First , think an easier version of this problem . How many distinct substrings are possible of a string S ?
Let’s solve an easier version of this easy problem . How many substrings are possible of a string S ?
For any p[i] i.e for any suffix , the number of possible substrings are (S.size() - p[i])
Then the ending position for possible substrings starts from p[i] and ends at S.size() - 1 (0 based indexing) . Hence the number of possible substrings are (S.size() - p[i])
Now , how many of them are distinct ?
We need to look into the LCP array for that . When we know LCP(i) , we know the length of Longest Common Prefix of suffix i and suffix i-1
Now , the ending position for possible substrings starts from p[i] + LCP(i) and ends at S.size() - 1 (0 based indexing)
Why p[i] + LCP(i) ?
Because in the
i-1th suffix we have already considered the substrings that start fromp[i],p[i] + 1, … ,p[i] + LCP(i) - 1
Now that we have solved all the easier versions . Let’s solve our problem :D
How many among the distinct substrings doesn’t contain another string as a substring ?
Let’s call this other string SUB .
What’s the thing that will change in our substring count ?
For any suffix , the ending position for possible substrings still starts from
p[i] + LCP(i). But this time we can’t end atS.size()-1as that could lead to have substring having the stringSUB. So , we need to find the new ending positions .
For this part we need to know the positions where our string SUB appears in our string S . We can use KMP for that . Also we can b_inary search on suffix array_ for that .
Anyway , now that you know where our string SUB occurs , you know that then ending position for that values will be p[i] + SUB.size() - 1 . As , if we go one more we would have considered the string SUB.
Now , we can solve for the rest of the positions easily .
Let’s see an example for this case.
Let , S = abacdaba & SUB = aba
abacdaba
0^^^^5^^
aba occurs in position 0 and 5 . What are the ending positions of 0 and 5 ?
For , 0 it is 0+2 = 2 . We can not end anywhere starting from 2 and beyond . So , we can make a ab but not aba
So , the ending positions become ,
abacdaba
2^^^^7^^
For other positions ?
abacdaba
27777788
Why ?
Well , figure that out yourself :p It’s really easy to see why
1 |
|