String Palindrome Queries
Time limit2sMemory limit256 MB
Maintain a lowercase string under block moves, reversals, and single-character insertions, answering after each change whether a given substring reads the same forwards and backwards.
- Level
Hard10 of 10
- Topics
- String, String matching, Implementation
- Solved
- No attempts yet
Problem
You are given a string of length . For , is the substring ; for , is the empty string.
Process queries in the order they are given. There are two kinds.
Question query. You are given integers and . Decide whether the substring is a palindrome.
Modification query. One of the following three.
- You are given integers , , . Cut into three parts , , , any of which can be empty. Concatenate the first part with the last one into , whose length is . Insert the middle part after the -th character of , forming , and set to .
- You are given integers and . Reverse the substring .
- You are given an integer and a character . Insert right before position , that is, set .
The third modification query makes the string one character longer, so means the length of the string just before the current query is processed.
Write a program that runs the queries above.
Input
The first line contains two space-separated integers and . (, )
The second line contains the characters of the initial string.
Each of the next lines contains one query.
- A question query has the form
Q i j. () - Modification query 1 has the form
M 1 i j k. (, ) - Modification query 2 has the form
M 2 i j. () - Modification query 3 has the form
M 3 i c, where is a single character. ()
On every line, is the length of the string just before that query is processed. You can assume that the string contains only lowercase letters of the English alphabet at all times. You may assume that the input is valid.
Output
For each question query, print the answer on its own line. Print YES if is a palindrome and NO otherwise. Do not print the quotes.
Hint
In the first example the modification queries change the string like this.
- Start:
banana - After
M 2 2 3:bnaana - After
M 2 5 6:bnaaan - After
M 3 7 b:bnaaanb - After
M 1 1 2 4:aaanbnb
The last modification query cuts out the block bn and puts it back after the first four characters of the remaining string aaanb.