QueryreuQ
InterviewTime limit1sMemory limit1024 MB
Maintain a string under append and pop-back operations, and after each operation print the number of palindromic substrings it contains.
- Level
Medium5 of 10
- Topics
- String, Dynamic programming, Implementation, Two pointers
- Solved
- No attempts yet
Problem
A string is a palindrome when reading it backwards gives the same string. For example "a", "aa", "appa" and "queryreuq" are all palindromes.
You start with an empty string and process two kinds of operations.
- Append one lowercase letter to the end of .
- Delete the last character of .
After each operation, count the palindromic substrings of the current string. For a string and integers , with , let be the substring of that runs from its -th character to its -th character. Count the pairs for which is a palindrome, and print that count.
Input
The input has two lines.
The first line has the number of queries .
The second line has the queries as a single string of length . Its -th character describes the -th query.
is either '-' or a lowercase English letter ('a', 'b', ..., 'z'). The quotation marks are not part of the input.
If is '-', delete the last character of . If is a lowercase letter, append to the end of .
The length of after every query is guaranteed to be at least 1.
Output
Print integers on one line, separated by a single space. The -th integer is the answer after the -th query.