Palindromes <Feb Circuits 19>

4

1 votes
Algorithms, String, Medium-Hard, Hashing algorithm
Problem

You have N strings S1,S2,S3,...,SN of lowercase characters.  You’ll need to support Q operations of two types. You have to process the operations/queries online. After each operations, you need to print how many strings are currently palindrome. A palindrome is a string that reads the same forward or backward.

Operattion Type 1: 1AB : That means SB will be added at the end of SA and SBwill be deleted. It is guaranteed that AB and SB will not be given in any other opertaion as it is deleted. 

Operattion Type 2: 2AKC : That means you need to insert character C at Kth position of SA

Note that the operations are encoded. That is, you need to write an online solution, please read the input format carefully.

Input Format 

First line will contain two integers N and Q (1<=N,Q<=105), they are number of strings and number of operations.

Each of next N lines will contain a string of lowercase characters. Summation of lengths of initial N strings <= 105.

Each of the next Q lines will describe a operation.

It contains one integer OP (1<=OP<=2) — the type of operation.

The given opeartions will be encoded in the following way: let Pr be the number of palindromes that you have printed after executing the previous operation (initially, Pr=0). 

  •  If OP=1, two integers follow, A and B (0<=A,B<230). First, do the following: A=APr and B=BPr (1<=A,B<=N). Now you'll execute the operation of first type, that is  SB will be added at the end of SA and SBwill be deleted.
  • If OP=2, two integers and a lowercase character follow, AK and C (0<=A,K<230). First, do the following: A=APr and K=KPr  (1<=A<=N,1<=K<=|SA|+1) . . Now you'll execute the operation of second type, which is insert character C at Kth position of SA.  If K=|SA|+1, then you need to append the character at the end of SA.

Output Format 

After completeing each operation, print one integer - that is currently how many strings are palindrome. 

Constraints

For 10 points: 1<=N,Q<=10 and summation of lengths of initial N strings <= 100.

For full point: Original constarints. 

 

Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

Initially there are three strings. Palindromic strings will be bold and deleted strings will be denoted by null.

After the 1st operation: yaaa, bbb, aaa (the answer is 2)

After the 2nd operation: yaaabbb, null, aaa (added at the end of, and is deleted, now it's denoted by 'null', it will be ignored from rest of the operations. And we have one palindrome)

After the 3rd operation: yaaabbbaaa, null, null (is added at the end of and is deleted. Now there is no palindrome, the answer is 0)

After the 4th operation: yaaabbbaaay, null, null (We have one palindrome)

After the 5th operation: yaaaxbbbaaay, null, null (No palindrome)

After the 6th operation: yaaaxbbbxaaay, null, null (Lastly, we have one palindrome)

Editor Image

?