QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 2048 MB 总分: 100

#15935. Initials

统计

在批改计算机科学作业时,助教喜欢按照学生在班级名册中的顺序来批改。不过,班级名册的排序方式相当奇怪:它是通过将名字(first name)拼接在姓氏(last name)之后(例如 Harry Potter 变成 PotterHarry),并将所有大写字母转换为小写字母来进行排序的。然后,按字典序对这些字符串进行排序,即为班级的顺序。每个学生都需要一个 Unix 目录,以便助教存放他们的作业。

由于助教很懒,他决定仅使用每个学生的缩写(姓氏的首字母,后跟名字的首字母)来为他们创建目录。但助教很快意识到,按照 Unix 排序目录的方式,学生们不再按正确的顺序排序,而且现在出现了重复的文件夹名称。糟糕!这必须予以解决。

对于每个名字,我们允许在缩写中添加来自原始名字的 $0$ 个或多个字母,但你必须总是使用前 $k$ 个未选择的字母。例如,Harry Potter 的缩写是 PH。添加 $2$ 个字母会使其变成 PotH,添加 $6$ 个字母会使其变成 PotterHa。

助教们向你寻求帮助。你的任务是编写一个程序,告诉助教们最少需要添加多少个字母,才能使所有学生(的目录)按正确的顺序排序,且所有生成的“扩展”缩写都是唯一的。

例如,在下方的样例输入中,(排序后的)缩写将是 (HA, HB, ZZ)。通过添加三个字母,我们得到 (HaB, HatA, ZZ),这就是正确的排序顺序。

输入格式

第一行包含一个整数 $2 \le N \le 1000$,表示学生的数量。

接下来有 $N$ 行,每行包含两个字符串:学生的名字(first name)和姓氏(last name)。名字和姓氏均包含至少一个字符,且仅由大写和小写英文字母组成。每个学生姓名(名字与姓氏)的总长度最多为 $80$ 个字符。没有两个学生会有相同的拼接姓名(如上文所述)。

输出格式

输出单行一个整数,表示为了使所有学生按正确顺序排序,最少需要添加的字母数量。

样例

输入样例 1

3
Bob Harris
Andrea Hat
Zanny Zan

输出样例 1

3

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.