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 threecoin 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 threecoin sequence appears. Obviously there are eight such threecoin 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 threecoin 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
hide comments
coolboy7:
20200628 12:22:26
it can be easily done using stl c++


qwertyrani3:
20200401 15:54:12
just use map...ac in one go 

srtpatil_2000:
20191230 17:26:00
just did it by bruteforce it worked


markaman:
20190103 16:06:24
Don't read comment there will be no comments


phoemur:
20181207 02:57:06
Should be moved to tutorial 

navidator:
20180606 00:13:39
Do not overthink or try to use substring algorithms. It is much easier, not only it is an O(N) algorithm, but also it is one pass. 

shashankpathak:
20180407 06:34:43
using pattern matching algorithm like KMP will be a good approach or not ?? Can we do it more simply?? 

ayusofayush:
20180224 11:40:43
very easy question...... 

chetan_555:
20171106 09:33:13
Had done a silly mistake .. used substring and got it 

themast3r:
20171011 17:52:30
easy peasy. AC in one go. 
Added by:  John Mario 
Date:  20110322 
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 