Skip to content

Commit

Permalink
Add solution to 08612
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyc committed Apr 20, 2012
1 parent 4980516 commit 955b17e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 08612_NY10A/NY10A.py
@@ -0,0 +1,11 @@
seq_len = 40
patterns = ['TTT', 'TTH', 'THT', 'THH', 'HTT', 'HTH', 'HHT', 'HHH']

P = input()
for p in xrange(P):
print raw_input(),
seq = raw_input().strip()
occurence = dict.fromkeys(patterns, 0)
for i in xrange(seq_len-2):
occurence[seq[i:i+3]] += 1
print ' '.join(str(occurence[p]) for p in patterns)
35 changes: 35 additions & 0 deletions 08612_NY10A/README.md
@@ -0,0 +1,35 @@
8612\. Penney Game
==================

## Problem code: NY10A

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
9 changes: 9 additions & 0 deletions 08612_NY10A/input
@@ -0,0 +1,9 @@
4
1
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
2
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
3
HHTTTHHTTTHTHHTHHTTHTTTHHHTHTTHTTHTTTHTH
4
HTHTHHHTHHHTHTHHHHTTTHTTTTTHHTTTTHTHHHHT

0 comments on commit 955b17e

Please sign in to comment.