Lotte10's blog

By Lotte10, 6 years ago, In English

Hello codeforces recently I've met the following problem https://codeforces.me/gym/101856/problem/B (problem B, Breaking the Curse).

For those who are too lazy to go through the link I will try to explain the problem as briefly as possible: We are given the two string $$$S, T$$$ and $$$Q$$$ queries. Queries are described by two integers $$$L$$$ and $$$R$$$, as a response we should print the number of intervals $$$i, j$$$ such that $$$L ≤ i ≤ j ≤ R$$$ and $$$S[i...j]$$$ is the substring of $$$T$$$.

Can someone give a hint or idea how to solve this ? (I would like to know the solution with Suffix Array:) )

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

my Idea would be the following:

1) create a string s3 = s1 + "$" + s2

2) build a suffix array on s3

3) for each query [L,R]:

for i in L...R: binary search for the 2 suffixes of s2 having the closest rank to the suffix of s1 starting at i, we have 2 such suffixes since we should take the first one having smaller rank and the first one having bigger rank

now get the lcp between the suffix at index i and those 2 suffixes of s2 and add the max to your answer

complexity O(q |s| log |s|) which should pass given the constraints

actually if it didn't pass just precompute for each suffix of s1 the closest suffixes of s2 as mentioned before (total |s| log |s|) and then use some prefix sum to answer queries in O(1)

  • »
    »
    6 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can you elaborate the part with 2 suffices, we have 2 such suffixes since we should take the first one having smaller rank and the first one having bigger rank. I did not get why there is the exactly 2 suffices

    • »
      »
      »
      6 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      okay suppose you have the following:

      s1: aacd

      s2: aaabaacd

      s3: aacd$aabaacd

      now sort the suffixes, you get:

      ...

      aaabaacd (belongs to s2)

      aacd$aabaacd (belongs to s1)

      aacd (belongs to s2)

      ....

      in this case when you wanna know how many substrings start at index 0 in s1 are also substrings of s2 you check the suffixes of s2 that are closest to this suffix

      the idea is that the closest suffix in s2 might be of smaller rank of my current suffix of s1 I am studying or of bigger rank. this is why you have 2 candidates

      in the above example it is clear that we should take the one having a bigger rank suppose s1 instead was aaac then we should have taken aaab instead.