logo AlgoBeat OnlineJudge
登录 注册

#212293. [ICPC 2023 WF] Alea Iacta Est

内存限制:512 MiB 时间限制:10000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You play a game with multiple fair six-sided dice. Each die's face displays a single symbol. The objective of the game is to roll the dice and create a valid word from the symbols on top of each die. If you cannot form a word, you may reroll the dice for another attempt.

Figure K.1: Five dice making a valid word corresponding to Sample Input 1.

Suppose there are five dice: one of them contains letters A, B, C, D, E, and P (abbreviated as ABCDEP), and the other dice contain letters AEHOXU, AISOLR, ABCDEF, and ABCSCC. The first roll yields the following letters on the tops of respective dice: P, X, R, E, and S. As it is impossible to arrange these letters into a valid word, you decide to keep the P, S, and E, and reroll the other dice, in an attempt to make words like PARSE, PAUSE, PHASE, POISE, PROSE, PULSE, or PURSE. The two dice yield E and A, resulting in the following five letters: P, E, A, E, and S. You still cannot think of a valid word, so you decide to keep four letters and reroll only the last die, which has three sides with letter C. By doing so, there is a chance that it will be possible to make a final valid word: PEACE, as shown in Figure K.1.

When you roll a die, it lands on any one of its faces with equal probability. What is the expected number of rolls needed to make a valid word, assuming you use an optimal strategy?

输入格式

The first line of input contains two numbers and , where () is the number of dice and () is the number of valid words in the dictionary. The following lines each have symbols, one for each face of the die. The final lines contain distinct valid words in the dictionary. Every word has exactly symbols.

All symbols in the input are either uppercase letters (A-Z) or digits (0-9).

输出格式

If it is possible to make a valid word, output the expected number of rolls needed to make a valid word when using an optimal strategy. Otherwise, output impossible. Your answer should have an absolute or relative error of at most .

样例

样例输入 1

5 8
ABCDEP
AEHOXU
AISOLR
ABCDEF
ABCSCC
PARSE
PAUSE
PHASE
POISE
PROSE
PULSE
PURSE
PEACE

样例输出 1

9.677887141

样例输入 2

2 1
AAAAAA
BBBBBB
AB

样例输出 2

1.0

样例输入 3

3 1
123456
123456
123456
666

样例输出 3

10.555444555

样例输入 4

2 1
ABCDEF
GHI234
AB

样例输出 4

impossible