NY10A - Penney Game

Penney’s game is a simple game typically played by two players.  One version of the game calls for each player to choose a unique three-coin sequence such as HEADS TAILS HEADS (HTH).  A fair coin is tossed sequentially some number of times until one of the two sequences appears.  The player who chose the first sequence to appear wins the game.

For this problem, you will write a program that implements a variation on the Penney Game.  You willread a sequence of 40 coin tosses and determine how many times each three-coin sequence appears.  Obviously there are eight such three-coin sequences: TTT, TTH, THT, THH, HTT, HTH, HHT and HHH. Sequences may overlap.  For example, if all 40 coin tosses are heads, then the sequence HHH appears 38 times.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow.  Each data set consists of 2 lines.  The first line contains the data set number N.  The second line contains the sequence of 40 coin tosses.  Each toss is represented as an upper case H or an upper case T, for heads or tails, respectively.  There will be no spaces on any input line.

Output

For each data set there is one line of output.  It contains the data set number followed by a single space, followed by the number of occurrences of each three-coin sequence, in the order shown above, with a space between each one.  There should be a total of 9 space separated decimal integers on each output line.

Example

Input:
4
1
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
2
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
3
HHTTTHHTTTHTHHTHHTTHTTTHHHTHTTHTTHTTTHTH
4
HTHTHHHTHHHTHTHHHHTTTHTTTTTHHTTTTHTHHHHT

Output:
1 0 0 0 0 0 0 0 38
2 38 0 0 0 0 0 0 0
3 4 7 6 4 7 4 5 1
4 6 3 4 5 3 6 5 6

Added by:John Mario
Date:2011-03-22
Time limit:0.800s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ACM Greater New York Regionals 2010

hide comments
2016-05-25 20:21:37
piece of cake for python users!
2016-05-21 13:13:35
pointers make it all easy
2016-05-21 11:09:54
@shan2new - :( Your hint didn't let me think enough.. Very first comment. !
2016-05-12 19:15:16
Can be done using Deterministic Finite Automata in 8 States in O(n) time and O(1) space.


Last edit: 2016-05-12 19:16:11
2016-03-06 13:43:49
AC in one go.. :)

Last edit: 2016-03-06 13:44:43
2016-02-18 15:34:59
EASY ONE..........!
2016-01-21 14:04:46 akash
It can be done without using any string functions with O(n) complexity, since there are only "two" choice ! ;)


Last edit: 2016-01-21 14:05:48
2016-01-06 11:10:47
One line AC solution Enumerable.Range, select and grouping. Thanks, C# LINQ!
2015-12-22 08:01:48 ABHISHEK RAJPUT
done without string hashing and maps in o(1),just learn some string functions and it will be done..
for reference see substr( ) and string compare. happy coding:)
2015-11-14 01:47:47 Siddhant Somani
Map STL. B)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.