Last active
January 19, 2016 11:54
-
-
Save cympfh/fb3032c5e1f2bbab157f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: utf-8 | |
N=100 # infty | |
def gen | |
ar = N.times.map{ %w(U D)[rand(2)]}.join '' # infinite seq | |
memo = {} | |
for i in 0..(N-3) | |
sub = ar[i..i+2] | |
memo[sub] = i + 1 if memo[sub] == nil | |
break if memo.size == 8 | |
end | |
memo | |
end | |
M=20000 # trial times | |
count={} | |
M.times { | |
m = gen | |
for k in m.keys | |
count[k] = 0 if count[k] == nil | |
count[k] += m[k] | |
end | |
} | |
for k in count.keys | |
puts "#{k} #{count[k] / M.to_f}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
アルファベット {U,D} 上の文字列を考える.
無限長の文字列 s (=s[1],s[2],...) 中に有限長文字列 w が m番目に現れるとは、
例. s=DUUUDDDUDUDUDU 中に DUD は7番目に出現する (たぶん漫画とは数え方が微妙に異なる)
問題. 長さ3のwが何番目に出現するか、sが一様分布で与えられる下でその期待値を求めよ.