Given each student's fixed semester points and a distribution of exam points, find the probability that the grade string avoids all forbidden substrings.
Hard8Dynamic programmingString matchingProbabilityTrieNo attempts yetTime limit1.5sMemory limit512 MBA university grades students on a scale of 0 to 100 points. A student earns 0 to 75 points during the semester and 0 to 25 points at the final exam. The final grade comes from the sum of the semester points and the exam points.
| Sum of points | Grade |
|---|---|
| 90-100 | A |
| 82-89 | B |
| 75-81 | C |
| 68-74 | D |
| 60-67 | E |
| 35-59 | FX |
A student who earns strictly fewer than 35 points during the semester is not allowed to take the exam. In this problem, assume the names of such students were already crossed out of the list.
Reading the grade column of the exam list from top to bottom spells out various "words". For example, if three consecutive students have point sums 92, 75 and 66, their grades are A, C and E, and they form the word ACE. A student whose grade is FX contributes two letters, first F and then X.
Nobody knows the exam results in advance. The lecturer does know how well each student understands the material and how hard the exam is, so for every student the lecturer estimates the probability, in percent, of each possible exam score: the probability of 0 points, of 1 point, of 2 points, and so on up to 25 points. That is 26 non-negative integers whose sum is 100. The points each student earned during the semester are fixed numbers from 35 to 75, with no probabilities attached.
The lecturer has strict taste and dislikes it when the word spelled by the grades contains an "unpleasant" string as a substring.
Write a program that finds the probability that no unpleasant string occurs.
The first line contains the number of students N. (3≤N≤4096)
Each of the next N lines contains 27 space-separated integers. The first one is that student's semester points, between 35 and 75. The other 26 are the probabilities, in percent, of scoring 0, 1, 2, ..., 25 points at the exam. Each probability is a non-negative integer and the 26 of them sum to 100.
The next line contains the number of unpleasant words K. (1≤K≤1024)
Each of the next K lines contains one unpleasant word. Every word consists of uppercase English letters only, its length is between 2 and 1024, and the lengths of the K words sum to at most 32768. The same word may be given more than once.
Print on one line the probability, in percent, that the lecturer is satisfied. Round it to six digits after the decimal point and always print all six digits. For a probability of 79.5 percent, print 79.500000. Use a decimal point, not a decimal comma.
In the first example the letter W never appears in a grade, so the word WAW can be ignored and only DE matters. The first student's point sum is at least 72+10=82, so that student cannot get a D. The word DE therefore appears only when the second student scores between 13 and 19 points and the third student scores between 5 and 12 points. The two probabilities are 8+8+7+6+5+4+3=41 percent and 3+4+5+6+7+8+8+9=50 percent. So DE appears with probability 0.41×0.5=0.205 and does not appear with probability 1−0.205=0.795, that is 79.5 percent.