QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 2048 MB Total points: 100

#10313. Morse Code

Statistics

摩尔斯电码是一种远距离通信的经典方式,但它存在一些会增加长消息传输时间的缺陷。

在摩尔斯电码中,字母表中的每个字符都被分配了一个由点(dot)和划(dash)组成的序列,且满足没有任何一个序列是另一个序列的前缀。为了传输一个字符串,需要按顺序发送每个字符对应的序列。发送一个划所需的时间是发送一个点所需时间的两倍。

你的字母表包含 $n$ 个字符,其中第 $i$ 个字符在语言中出现的频率为 $f_i$。你的任务是设计一种摩尔斯电码编码方案,为每个字符分配一个由点和划组成的序列,使得单个字符的期望传输时间最小。换句话说,你需要最小化 $f_1t_1 + f_2t_2 + \dots + f_nt_n$,其中 $t_i$ 是分配给第 $i$ 个字符的序列所需的传输时间。

输入格式

第一行包含一个整数 $n$ ($2 \le n \le 200$),表示字母表中的字符数量。

第二行包含 $n$ 个实数 $f_1, f_2, \dots, f_n$ ($0 < f_i < 1$),$f_i$ 是第 $i$ 个字符的频率。所有 $f_1, f_2, \dots, f_n$ 的值均精确给出小数点后四位。所有频率之和恰好为 1。

输出格式

输出 $n$ 行,每行包含一个由点 . 和划 - 组成的字符串。第 $i$ 行对应你分配给第 $i$ 个字符的序列。

如果存在多种能达到最小期望传输时间的有效分配方案,输出其中任意一种即可。

样例

输入 1

3
0.3000 0.6000 0.1000

输出 1

-.
.
--

说明 1

字母表包含三个字母,假设为 $a$、$b$ 和 $c$,其频率分别为 0.3、0.6 和 0.1。在最优分配中,我们将 $a$ 分配为 ‘-.’,$b$ 分配为 ‘.’,$c$ 分配为 ‘--’。这使得每个字符的期望传输时间为 $0.3 \cdot 3 + 0.6 \cdot 1 + 0.1 \cdot 4 = 1.9$ 个时间单位,这是最优的。

作为对比,分配 $a \to$ ‘..’,$b \to$ ‘-’,$c \to$ ‘.-’ 的期望传输时间为 $0.3 \cdot 2 + 0.6 \cdot 2 + 0.1 \cdot 3 = 2.1$。分配 $a \to$ ‘-’,$b \to$ ‘.’,$c \to$ ‘..’ 的期望传输时间更低,但它是无效的,因为 ‘.’ 是 ‘..’ 的前缀。

输入 2

3
0.3000 0.4500 0.2500

输出 2

..
-
.-

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.