Xsquare And Chocolates Bars
Practice
4.2 (38 votes)
Approved
Dynamic programming
Easy
Implementation
Open
Problem
38% Success 10588 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Xsquare loves to eat chocolates like we all do. Xsquare's father has given him a chocolate bar B of length N where each chocolate piece is either of strawberry flavour S or of caramel flavour C. Xsquare's mom does not want him to eat all the chocolates as she thinks that consumption of too many chocolates will make him chocoholic. Therefore, she decided to take some chocolates back from him. She has some empty boxes to keep chocolates. Each box can contain exactly three chocolates. No box can contain all the chocolates of same flavour. She has ordered Xsquare to return her back, the longest contiguous sub-bar of the original chocolate bar such that she can place every three consecutive chocolates from that contiguous chocolate bar in an empty box. Each empty box can accomodate exactly three chocolates, neither less, nor more. A sub-bar is defined as contiguous peices of chocolates in some particular range.

Xsquare agrees to whatever his mom has ordered. Now, he is wondering how many chocolates he is able to eat at the end after he returns the longest contiguous sub-bar.

Help him in accomplishing this task.

Sample Example Image :

enter image description here

Input :

First line of input contains a single integer T denoting the number of test cases. Each of the next T line of input contains a string B denoting the chocolate bar, where ith character in the jth line denotes the flavour of ith chocolate piece in jth chocolate bar.

Output :

Output consists of T lines, each containing number of chocolates Xsquare manages to eat at the end.

Constraints :

  • 1 ≤ T ≤ 105
  • 1 ≤ |B| ≤ 106
  • String B consists of character 'S' and 'C' only.
  • sum of |B| over all test cases does not exceed 106

Subtasks :

  • sum of |B| over all test cases does not exceed 106 : ( 40 pts )
  • sum of |B| over all test cases does not exceed 106 : ( 60 pts )

Warning :

Prefer to use scanf / printf instead of cin / cout .

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
1040 votes
Tags:
Ad-HocEasyImplementation
Points:20
47 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingEasyOpen
Points:20
21 votes
Tags:
ApprovedBrute-force searchData StructuresDynamic ProgrammingEasyOpen