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.


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.


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.



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
Gustav: 2015-10-19 10:39:59

Don't forget a space after every line in the output.

coder_shishir: 2015-10-02 10:58:36

good problem to learn string hashing.....!!!!...plzz dont't try brute force...

Sarthak Munshi: 2015-09-20 12:38:14

what is N ? confusing question !

Mohit: 2015-09-20 04:55:49

Very easy question just use substr()

sneh sajal: 2015-08-30 22:36:07

try thinking using string functions n juss bruteforce :)

Harsh Vardhan Ladha: 2015-08-12 09:28:23

last character of every output line is a space :/
caused 2 WAs

karthik1997: 2015-07-06 09:01:55

2 wa's because of using - instead of + symbol :( :P
use multi strings to store the different sequences of arrays .
and int is enough
good question

Shubham Sinha: 2015-03-23 20:53:24

My 50th.
Don't overtry it. Just try the brute forcing.

MKM: 2015-01-28 07:58:26

learned a new concept about strings

darol: 2015-01-22 04:43:02

dont forget space after the last output char in line costed me few WA. Cheers!

Added by:John Mario
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