# 字典序

**字典序**（又称词典序或字汇序）是一种对取自有序集合的元素序列进行排序的方式：逐位比较序列中的元素，在第一个出现差异的位置上作出判定，恰如字典中单词的排列方式。它将字母表顺序从字母串推广到了元组、字符串以及任意有序集合的积。<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup><sup> • </sup><sup>[2](https://mathworld.wolfram.com/LexicographicOrder.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 对于有序集合上的序列，当在第一个不同位置 \( k \) 处有 \( x_k < y_k \) 且之前所有位置均相同，则 \( x < y \)（“首差”原则）<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup> |
| 起源 | 对于有限指标集的情形，最早由格奥尔格·康托尔（Georg Cantor）在定义全序集序型的积时提出<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup> |
| 序论性质 | 有限多个良序集的字典序积仍是良序的；链的字典序积仍是链<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup> |
| 字符串变体 | 短字典序（长度-字典序）先比较长度，再对等长字符串施加字典序<sup>[3](https://www.cs.ucdavis.edu/~doty/ecs120slides/01b_Strings-deck.html)</sup> |
| 示例 | {1, 2, 3} 的排列按字典序为 123, 132, 213, 231, 312, 321<sup>[2](https://mathworld.wolfram.com/LexicographicOrder.html)</sup> |
| 主要计算用途 | 后缀数组（对文本所有后缀进行字典序排序）以及组合对象的排名/反排名<sup>[4](https://epubs.siam.org/doi/10.1137/0222058)</sup><sup> • </sup><sup>[5](https://www.tandfonline.com/doi/abs/10.1080/00207168508803468)</sup> |
| 文本处理中的注意事项 | 简单的二进制比较无法得到文化上正确的词序；Unicode 排序算法（UTS #10）规定了根据区域设置进行比较的方法<sup>[6](https://unicode.org/reports/tr10/)</sup> |

## 形式定义

设 \( X = \prod_{\alpha \in L} X_\alpha \) 为偏序集 \( X_\alpha \) 的直积，其中指标集 \( L \) 是良序的，即每个非空子集都有最小元。为在 \( X \) 上定义字典序，规定 \( (x_\alpha) \leq (y_\alpha) \) 恰好当两个序列在每个 \( \alpha \in L \) 处均相同，或者存在某个 \( \alpha \in L \) 满足 \( x_\alpha < y_\alpha \) 且对所有 \( \beta < \alpha \) 有 \( x_\beta = y_\beta \)。装备此序的集合 \( X \) 称为集合族 \( X_\alpha \) 的字典序积或序数积；当所有因子相同，即 \( X_\alpha = Y \) 时，所得称为 \( Y \) 的序数幂，记作 \( {}^L Y \)。<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup>

对于以自然数索引的序列的具体情形，\( (x_1, \ldots, x_n, \ldots) < (y_1, \ldots, y_n, \ldots) \) 意味着存在某个 \( k \)，使得 \( x_k < y_k \) 且对所有 \( i < k \) 有 \( x_i = y_i \)。<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup> 对于两个各自带有序关系的有限集 \( A \) 和 \( B \)，笛卡尔积 \( A \times B \) 上的序规定：\( (a_1, b_1) < (a_2, b_2) \) 当且仅当 \( a_1 <_A a_2 \)，或 \( a_1 = a_2 \) 且 \( b_1 <_B b_2 \)；该定义通过递归利用 \( A \times B \times C = A \times (B \times C) \) 推广到任意长度的积。<sup>[2](https://mathworld.wolfram.com/LexicographicOrder.html)</sup>

该定义也可对任意偏序指标集 \( L \) 给出，而不仅限于良序集，尽管此时所得关系未必是通常意义上的序。<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup>

## 序论性质

两条结构性事实决定了字典序的行为方式。有限多个良序集的字典序积本身是良序的，因此每个非空子集都有最小元，且不可能存在无限下降链。链（全序集）的字典序积仍是链，因此任意两个元素均可比较。<sup>[1](https://encyclopediaofmath.org/wiki/Lexicographic_order)</sup>

当指标集为无限时，这些保证会减弱。在自然数的有限序列集合上，朴素字典序并非良序：序列

\[ 1 \succ 01 \succ 001 \succ 0001 \succ \cdots \]

构成一条无限严格下降链，因为每个序列与下一个序列在第一个位置即不同。<sup>[7](https://math.stackexchange.com/questions/1133423/finite-sequences-linear-order-lexicographic-order-well-order-cardinal)</sup> 这一缺陷正是形式语言理论改用修正序的原因之一。

## 字符串与短字典序

当被比较的元素是字母表 \( \Sigma \) 上带有自然符号序的字符串时（例如 \( 0 < 1 \) 或 \( a < b < \cdots < z \)），朴素字典序与词典序一致：两个等长字符串逐位比较，而作为另一字符串前缀的字符串排在其前。<sup>[3](https://www.cs.ucdavis.edu/~doty/ecs120slides/01b_Strings-deck.html)</sup><sup> • </sup><sup>[8](https://www.seas.upenn.edu/~cis2620/notes/cis262sl1.pdf)</sup>

**短字典序。** 由于 \( \Sigma^* \) 上的朴素字典序并非良序，一种常见的变体先比较长度：\( x < y \) 当且仅当 \( |x| < |y| \)，或 \( |x| = |y| \) 且 \( x \) 在第一个不同位置处按词典序先于 \( y \)。这称为短字典序或长度-字典序，它确实良序化 \( \Sigma^* \)。在 \( \{0, 1\} \) 上，它列出字符串为 \( \epsilon, 0, 1, 00, 01, 10, 11, 000, \ldots \)<sup>[3](https://www.cs.ucdavis.edu/~doty/ecs120slides/01b_Strings-deck.html)</sup><sup> • </sup><sup>[9](https://cic.tju.edu.cn/faculty/yangyajun/course/FormalL/ch00.pdf)</sup> 这种枚举还给出了 \( \Sigma^* \) 与自然数之间的双射，其中 \( k \) 符号字母表上长度为 \( n \) 的字符串数目为 \( k^n \)。<sup>[8](https://www.seas.upenn.edu/~cis2620/notes/cis262sl1.pdf)</sup>

应用于其他组合族时，同样的首差规则给出有用的列举：\( \{1, 2, 3\} \) 的子集按字典序为 \( \{\} \), \( \{1\} \), \( \{1,2\} \), \( \{1,2,3\} \), \( \{1,3\} \), \( \{2\} \), \( \{2,3\} \), \( \{3\} \)。<sup>[2](https://mathworld.wolfram.com/LexicographicOrder.html)</sup>

## 枚举、排名与反排名

字典序是生成组合对象的标准列举顺序。西澳大利亚大学的 M.C. Er 于 1985 年证明，组合可以表示为二进制串，使得组合与码字之间的双射在字典序下是保序的（单调的），并给出了按字典序枚举所有组合的递归算法，以及计算对象在字典序列举中的位置索引或由该索引反求对象的排名与反排名算法。<sup>[5](https://www.tandfonline.com/doi/abs/10.1080/00207168508803468)</sup> 1987 年的一篇配套论文给出了按字典序枚举从 \( n \) 个对象中取 \( r \) 个的排列的 \( \Theta(r) \) 算法。<sup>[10](https://www.tandfonline.com/doi/abs/10.1080/00207168708803603)</sup>

生成过程在空间和时间上均可做到高效。以色列理工学院的阿隆·伊泰（Alon Itai）于 2001 年证明，排列和 \( p \)-组合可以按字典序生成，除存放对象的数组外仅需 \( O(1) \) 额外存储，每项分摊时间为 \( O(1) \)；他还证明了任何无记忆的 \( p \)-组合生成算法都需要非常数时间，因此若不付出时间代价，适度的空间需求无法进一步改进。<sup>[11](https://www.scielo.br/j/jbcos/a/kn9PDHZJHhpthdkF5FWqbnc/?lang=en)</sup> 排名通过枚举编码与信息论相联系：集合 \( S \) 中的每个字被赋予一个长度至少为 \( \log_2 |S| \) 的定长二进制码字；对于排列、有序分拆及相关族，已开发出每字母使用 \( O((\log n)^{\mathrm{const}}) \) 位运算的快速排名方法。<sup>[12](https://arxiv.org/abs/cs/0601069)</sup>

## 后缀数组与文本索引

在字符串算法中，字典序是后缀数组的组织原则，该数据结构由乌迪·曼伯（Udi Manber）和吉恩·迈尔斯（Gene Myers）于 1990 年提出。后缀数组存储文本所有后缀按字典序排列的起始位置，模式搜索归结为对该有序列表的二分搜索，可在 \( O(P + \log N) \) 时间内回答“\( W \) 是否为 \( A \) 的子串？”这一问题，其中 \( P \) 为模式长度，\( N \) 为文本长度。实践中，后缀数组占用的空间为后缀树的五分之一到三分之一，代价是对于小有限字母表需要 \( O(N \log N) \) 的构造时间，而后缀树的最坏情形构造时间为 \( O(N) \)。<sup>[4](https://epubs.siam.org/doi/10.1137/0222058)</sup> 后缀数组常辅以最长公共前缀（LCP）数组，该数组存储每对字典序相邻后缀的最长公共前缀长度。<sup>[13](https://www.cs.cmu.edu/~15451-f20/LectureNotes/suffix-arrays.pdf)</sup>

## 计算中的排序

对编码字符进行朴素的字典序比较并不能重现字典中单词的排列方式。Unicode 标准指出，不可能在编码中安排字符使得简单的二进制字符串比较产生所需的排序顺序，因为排序规则因文化而异：瑞典语将字母 ä 视为排在 z 之后的独立字母，德语将其按 ae 排序或排在 a 之后，而西班牙语传统上将二合字母 ll 排在 l 和 m 之间。<sup>[14](https://www.unicode.org/versions/Unicode12.1.0/ch05.pdf)</sup> Unicode 排序算法（UTS #10）改为将字符映射为称为排序键的排序权重序列，然后直接比较这些权重；它处理诸如在斯洛伐克语中将“ch”视为单个字母的缩合情形，并支持取自 Unicode 通用区域数据仓库的区域特定定制。<sup>[6](https://unicode.org/reports/tr10/)</sup>

## References

1. [Lexicographic order, Encyclopedia of Mathematics](https://encyclopediaofmath.org/wiki/Lexicographic_order)
2. [Lexicographic Order, Wolfram MathWorld](https://mathworld.wolfram.com/LexicographicOrder.html)
3. [ECS 120 Theory of Computation: Strings and languages, UC Davis](https://www.cs.ucdavis.edu/~doty/ecs120slides/01b_Strings-deck.html)
4. [Udi Manber and Gene Myers (1990). Suffix Arrays: A New Method for On-Line String Searches. SIAM Journal on Computing.](https://epubs.siam.org/doi/10.1137/0222058)
5. [M.C. Er (1985). Lexicographic ordering, ranking and unranking of combinations. International Journal of Computer Mathematics 17(3-4).](https://www.tandfonline.com/doi/abs/10.1080/00207168508803468)
6. [Unicode Technical Standard #10: Unicode Collation Algorithm](https://unicode.org/reports/tr10/)
7. [Finite sequences, linear order, lexicographic order, well order, cardinal, Mathematics Stack Exchange](https://math.stackexchange.com/questions/1133423/finite-sequences-linear-order-lexicographic-order-well-order-cardinal)
8. [Introduction to the Theory of Computation, University of Pennsylvania](https://www.seas.upenn.edu/~cis2620/notes/cis262sl1.pdf)
9. [Mathematical Notions and Terminology, Formal Languages course notes](https://cic.tju.edu.cn/faculty/yangyajun/course/FormalL/ch00.pdf)
10. [Lexicographic enumeration, ranking and unranking of permutations of r out of n objects. International Journal of Computer Mathematics 23(1).](https://www.tandfonline.com/doi/abs/10.1080/00207168708803603)
11. [Alon Itai (2001). Generating permutations and combinations in lexicographical order. Journal of the Brazilian Computer Society 7(3).](https://www.scielo.br/j/jbcos/a/kn9PDHZJHhpthdkF5FWqbnc/?lang=en)
12. [Fast Enumeration of Combinatorial Objects, arXiv](https://arxiv.org/abs/cs/0601069)
13. [Suffix Arrays, Carnegie Mellon University 15-451 lecture notes](https://www.cs.cmu.edu/~15451-f20/LectureNotes/suffix-arrays.pdf)
14. [The Unicode Standard, Version 12.0, Chapter 5: Sorting and Searching](https://www.unicode.org/versions/Unicode12.1.0/ch05.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Combinatorics › Enumerative combinatorics › Combinatorics on words › Finite words and word structures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lexicographic-order

English original: https://www.edgepedia.org/lexicographic-order
