NOTE. No solution technology based on recording only the number of subscripted subscripts starting or ending at each position can work, because it cannot necessarily distinguish (at least) the following two cases (found from computer search) that have the same UpTo arrays [ ] and From [], but different sets of PSes:
ijegajaei dacedcadc |-------| |-----| |-----| |-----| |-| |---|
Although the attempt at a solution that I describe below is based on this erroneous idea, if someone has no objection, I think I will leave this information here if it saves someone else from this path.
Here, a different approach is used that meets each request [l, r] in O (1) time and online (that is, we can respond to each request as it arrives - we do not need to view them all, and then reorder them ) after O (| A | n log n) preprocessing , where | A | = 26 is the size of the alphabet. He needs the space O (| A | n).
Overview
The main idea is to calculate the number of interlinear substrings ending in i or any earlier position (let me call this UpTo [i]) at the pre-processing stage for each position i, as well as the number of initial substrings in i or in any later position (let me call it From [i]). As soon as we get this information, note that for any query [l, r] UpTo [r] + From [l] counts each subscript exactly once, with the exception of palindromic substrings starting with or after l and ending with l before or before r, which are counted twice. Therefore, if we subtract the total number of subscript substrings from the expression UpTo [r] + From [l], we get exactly what we are looking for - the number of subscript subscripts starting with or after l and ending before or before p. It is clear that we can store UpTo [] and From [] in two arrays of length n + 1 and perform this calculation O (1) times per request; Now the question is how to calculate the entries in these two tables.
Suffix Minimum Subscript Constants
First, a few definitions (mostly taken from my previous incorrect answer):
Call the range [l, r] valid if it is a palindrome, i.e. if no more than one of the characters of this substring has an odd frequency. Call the valid range, even if all the characters in it have an even frequency, otherwise call it odd and, in particular, c-odd, where c is a unique character with an odd frequency.
Here we will deal with counting the number of EndsAt [i] subscript substrings (PS) that end at a specific position i. (It is clear that we can calculate UpTo [] from the current sum of EndsAt [], and we can calculate From [] by working with the return line.) We will separately count the number EvenEndsAt [i] even PS, which ends in i.
Consider an even PS starting with j and ending with i> j. Either this is the shortest even PS ending with i, in which case we call it minimal, or there is a shorter even PS ending with i and starting with some j '> j. The following observation is the key to efficiently calculating EvenEndsAt [i]:
- If [j, i] and [j ', i] are both even PSes and j <j' i, then [j, j'-1] is also an even PS. (IOW: if the line is an even PS and has the correct suffix, which is an even PS, then removing this suffix leaves a line that is also an even PS.)
This follows from the fact that subtracting an even number from an even number always gives an even number. This means that if we can determine the minimum even PS [k, i] ending on some i, then we can calculate EvenEndsAt [i] using EvenEndsAt [i] = 1 + EvenEndsAt [k-1].
Finding a minimum even PS ending in i
How to find this minimum even PS [k, i] (if it exists)? As mentioned in David Eisenstat’s answer, to determine if [a, b] is an even PS, we need to take care of the parity (oddness or evenness) of the cumulative symbol frequencies in position a-1 and position b. For an even PS, these parities should be equal for each of the 26 letters. I will also use the signature to describe the set of letters with an odd frequency so far.
So, what we can do is scan forward along the line, maintaining (in the hash table or balanced tree) the most recent (i.e. rightmost) location of each signature that we have seen so far. Please note that we do not need to record all 2 ^ 26 possible signatures - it is enough to record only those signatures that we actually saw, which limits the use of the O (n) space.
Odd PS
A similar observation is performed for odd PSes. We just need to be careful to count the c-odd PSes ending at each position I separately for each c, so we won’t count any odd PS twice.
Consider a c-odd PS starting with j and ending with i> j. Either this is the shortest c-odd PS ending in i, in which case we call it minimal, or there is some shorter c-odd PS ending in i and starting with some j '> j. We can calculate the total number of c-odd PSes ending in i using the following observation:
- If [j, i] and [j ', i] are c-odd PSes (note: for the same character c!), With j <j' i, then [j, j'-1] is even PS . (IOW: if a line is an odd PS and has its own suffix, which is an odd PS and in which the same character has an odd frequency, then removing this suffix leaves a line that is an even PS.)
The predictions are only a little more complicated than last time: if we have an odd number and some even numbers, and we subtract an odd number from an odd number and (possibly different) even numbers from each even number, we must complete with even numbers. Thus, if [k, i] is the minimum c-odd PS ending in position i, then the total number of c-odd PSes ending in position i is given by 1 + EvenEndsAt [k-1].
pseudo code
Here is the pseudo code for calculating the UpTo [] table. As mentioned earlier, From [] can be calculated by doing this on an inverted line, and then each request can be answered within O (1) (either UpTo [n] or From [1] will give the total number of PSes in the whole line, necessary for the final subtraction). Here I use indexes based on 1:
FindPS(): Dictionary lastPosOfSignature