-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathweak_typing_chapter_3.py
69 lines (63 loc) · 1.82 KB
/
weak_typing_chapter_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright (c) 2021 kamyu. All rights reserved.
#
# Facebook Hacker Cup 2021 Round 1 - Problem A3. Weak Typing - Chapter 3
# https://www.facebook.com/codingcompetitions/hacker-cup/2021/round-1/problems/A3
#
# Time: O(N)
# Space: O(1)
#
# slower but easier to figure out
#
from itertools import izip
def matrix_mult(A, B):
ZB = zip(*B)
return [[sum(a*b % MOD for a, b in izip(row, col)) % MOD for col in ZB] for row in A]
def identity_matrix(N):
return [[int(i == j) for j in xrange(N)] for i in xrange(N)]
def weak_typing_chapter_3():
N = input()
W = raw_input().strip()
result = identity_matrix(6)
for c in W:
result = matrix_mult(result, T[c] if c != '.' else result)
return matrix_mult([[0, 0, 0, 0, 0, 1]], result)[0][0]
# state: [result, accu, os, xs, unknowns, 1]
# case 'F':
# - new_accu = accu
# - new_os = os
# - new_xs = xs
# - new_unknowns = unknowns + 1
# case 'O':
# - new_accu = accu + xs
# - new_os = os + xs + unknowns + 1
# - new_xs = 0
# - new_unknowns = 0
# case 'X':
# - new_accu = accu + os
# - new_os = 0
# - new_xs = os + xs + unknowns + 1
# - new_unknowns = 0
# new_result = result + new_accu
T = {
'F': [[1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1]],
'O': [[1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1]],
'X': [[1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1]]
}
MOD = 10**9+7
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, weak_typing_chapter_3())