DokjaKim's blog

By DokjaKim, history, 87 minutes ago, In English

I wanted to share an approach to solve the C — Range Search Query using prefix sum with 0/1's.

I used AI to help phrase some parts in English, so there might still be some awkward expressions. The approach is my own.

Read the question above before trying to figure out the approach.

t = "bc"; s ="babcdabc"; w = 00100010;

here '1' means the occurrence of t starts at that position. and made a prefix sum array over w.

so now ps: w: — 0 0 1 0 0 0 1 0 ps: 0 0 0 1 1 1 1 2 2

now this will tell us how many occurrences of t starts in position 1 to i.

now trying this out!! l=4, r=8 substring is "cdabc" we cant allow a match at position 8 because t="bc" has length 2, and match would exceed outside of query's boundary 'r'.

so right=r-t.length()+1; here right=7

so now we count how many occurrences start in this range count=ps[7]-ps[3] =2-1 =1 since count>0 so 1 or more occurrences starts inside the range l to r. So its a Yes.

Full text and comments »

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