Under construction

Still being built. Things may change

May I Add a Letter?

Time limit2.5sMemory limit1024 MB

Summary
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 NN, and you must perform a sequence of QQ 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 NN, the initial value of S. Each character of S is an uppercase letter.

The second line contains the string U of length QQ, 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: 1≤N≤1051 \le N \le 10^5, 1≤Q≤1051 \le Q \le 10^5.

출력

Output Q+1Q + 1 lines. Line 1 gives the answer for the initial value of S. Line i+1i + 1 gives the answer after applying the first ii updates (i=1,2,…,Qi = 1, 2, \ldots, Q).

Examples3

  1. Example 1

    Input
    ABABC
    CC-
    
    Expected output
    3
    4
    5
    4
    
  2. Example 2

    Input
    ABAB
    A--CC
    
    Expected output
    3
    5
    3
    1
    1
    2
    
  3. Example 3

    Input
    HAVE
    FUN
    
    Expected output
    0
    0
    0
    0