The Knuth-Morris-Pratt (KMP) algorithm produces such a DFA (with O(n) states) in O(n) time (Knuth et al., 1977).
computing the infix probabilities of each of w's prefixes takes O(n(n|Q P |) m ) time, where m is the matrix multiplication constant 2 , as at each step one needs to rebuild the entire intersection automaton for the current prefix and compute an inverse on its transition matrices.