from fractions import Fraction n, m = map(int, input().split()) inp = input() inp = '.' + inp # print(n, m) # print(inp) ans = Fraction() idx = 1 qcount = 0 for x in inp: qcount += (x == '?') # print(qcount) while idx <= m: if inp[idx] == '.': idx += 1 continue nidx = idx while nidx <= m and inp[nidx] != '.': nidx += 1 # inp[idx..nidx - 1] contrib = [Fraction()] * (m + 1) # print("solve range", idx, nidx) for r in range(idx, nidx): c = inp[r] prob = Fraction(1) for p in range(r, -1, -1): dl = r - p if inp[p] == '?': # this can be end prob = prob / 2 contrib[r] += prob * contrib[p - 1] if dl != 2 and dl != 0: # we can use this on for the right one contrib[r] += prob # or continue elif inp[p] == '.': if dl != 2: contrib[r] += prob break # print(contrib[nidx - 1]) ans += contrib[nidx - 1] idx = nidx nume = ans.numerator mian = ans.denominator print('{}/{}'.format(nume, mian))
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 | from fractions import Fraction n, m = map(int, input().split()) inp = input() inp = '.' + inp # print(n, m) # print(inp) ans = Fraction() idx = 1 qcount = 0 for x in inp: qcount += (x == '?') # print(qcount) while idx <= m: if inp[idx] == '.': idx += 1 continue nidx = idx while nidx <= m and inp[nidx] != '.': nidx += 1 # inp[idx..nidx - 1] contrib = [Fraction()] * (m + 1) # print("solve range", idx, nidx) for r in range(idx, nidx): c = inp[r] prob = Fraction(1) for p in range(r, -1, -1): dl = r - p if inp[p] == '?': # this can be end prob = prob / 2 contrib[r] += prob * contrib[p - 1] if dl != 2 and dl != 0: # we can use this on for the right one contrib[r] += prob # or continue elif inp[p] == '.': if dl != 2: contrib[r] += prob break # print(contrib[nidx - 1]) ans += contrib[nidx - 1] idx = nidx nume = ans.numerator mian = ans.denominator print('{}/{}'.format(nume, mian)) |