May I Add a Letter?
Time limit2.5sMemory limit1024 MB
Given a string that changes by appending a character or deleting the last one, report after each update how many distinct substrings occur at least twice.
- Level
Hard8 of 10
- Topics
- String, String matching, Segment tree
- Solved
- No attempts yet
문제
You have a string S of length , and you must perform a sequence of updates of two types on S:
- Append a given character to the end of S.
- Delete the last character from S.
Initially, and after each update, you must calculate the number of distinct strings that occur at least twice as substrings of S.
For example, if S is initially ABABC, the answer is 3, because A, B, and AB each occur twice as substrings of S. If you append the character C, S becomes ABABCC and the answer is 4, because C now occurs twice too. If you append C again, S becomes ABABCCC and the answer is 5, because CC now occurs twice. If you then perform a delete operation, S becomes ABABCC and the answer is 4 again.
입력
The first line contains the string S of length , the initial value of S. Each character of S is an uppercase letter.
The second line contains the string U of length , the updates to perform. Each character of U is either an uppercase letter, which means that letter must be appended, or the character - (hyphen), which denotes a delete operation. The updates must be applied in the order they appear in U. It is guaranteed that delete operations are never applied to an empty string.
Constraints: , .
출력
Output lines. Line 1 gives the answer for the initial value of S. Line gives the answer after applying the first updates ().