Distinct Substring Queries
Time limit2sMemory limit512 MB
Maintain a string under push-back and pop-front operations, reporting the number of distinct substrings after each of up to a million queries.
- Level
Hard9 of 10
- Topics
- String, String matching, Sorting, Trie
- Solved
- No attempts yet
Problem
The string is empty at the start. Write a program that performs the following two kinds of queries in order.
+ c: append the character to the end of . is a lowercase English letter.-: remove the first character of .
Right after each query, the length of is always positive.
After each query, you must find the number of distinct substrings of at that moment.
Input
The first line contains the number of queries ().
Each of the next lines contains one query.
Output
For each query, find the number of distinct substrings of right after that query. Print the sum of these numbers over all queries, modulo .
Note
In the first example, the state after each query is as follows.
- = a. It has 1 distinct substring: a.
- = ab. It has 3 distinct substrings: a, b, ab.
- = aba. It has 5 distinct substrings: a, b, ab, ba, aba.
- = abaa. It has 8 distinct substrings: a, b, ab, ba, aa, aba, baa, abaa.
- = baa. It has 5 distinct substrings: a, b, ba, aa, baa.
- = aa. It has 2 distinct substrings: a, aa.
- = a. It has 1 distinct substring: a.
- = aa. It has 2 distinct substrings: a, aa.
The sum is , and , so the answer is 27.