# 连接

**连接**（concatenation）是把两个字符串首尾相接的运算：给定长度为 \( m \) 的字符串 \( a \) 和长度为 \( n \) 的字符串 \( b \)，它们的连接是长度为 \( m+n \) 的字符串 \( a_1a_2\cdots a_m b_1b_2\cdots b_n \)。它是形式语言理论的基本运算，在其中扮演着算术中加法所扮演的角色；凡有文本拼接之处，从 shell 命令到正则表达式，都能见到它的实际应用。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

| 关键事实 | 细节 |
|---|---|
| 定义 | 对长度为 \( m \) 的字符串 \( a \) 和长度为 \( n \) 的字符串 \( b \)，连接 \( a+b \) 为 \( a_1a_2\cdots a_m b_1b_2\cdots b_n \)，长度为 \( m+n \)<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup> |
| 代数结构 | 字母表 \( A \) 上全体字符串构成的集合 \( A^* \) 在连接运算下构成一个幺半群，以空串 \( \lambda \) 为单位元<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup> |
| 交换性 | 只要字母表含有多于一个字母，连接就不满足交换律<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup> |
| 语言连接 | 对语言 \( L_1 \) 和 \( L_2 \)，\( L_1L_2 = \{xy : x \in L_1,\ y \in L_2\} \)<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup> |
| Kleene 星号 | \( L^* = L^0 \cup L^1 \cup L^2 \cup \cdots \)，即将 \( L \) 中零个或多个字符串连接起来所能形成的全部字符串，其中 \( L^0 = \{\lambda\} \)<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup><sup> • </sup><sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup> |
| 大小 | 即便字母表 \( \Sigma \) 是有限的，\( \Sigma^* \) 与 \( \Sigma^+ = \Sigma^* - \lambda \) 也是无限的<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup> |
| 记号 | 所用符号包括 \( + \)（Python、SageMath）、并置 \( ab \) 以及 \( \cdot \)；空串也写作 \( \varepsilon \)<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup><sup> • </sup><sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup> |

## 字符串与自由幺半群

固定一个字母表 \( A \)，即一个有限的符号集合。\( A \) 上长度为 \( n \) 的字符串是从 \( A \) 中取出的 \( n \) 个字母组成的序列，写作 \( a_1a_2\cdots a_n \)，其中字母可以出现任意多次。长度为 0、不含任何字母的空串 \( \lambda \) 也算在字符串之列；它与空集不是同一个对象，尽管两者在许多场合下表现相似。\( A \) 上全体字符串的集合写作 \( A^* \)，长度为 \( n \) 的字符串的集合写作 \( A^n \)。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

在连接运算下，\( A^* \) 是一个幺半群：该运算满足结合律，因为把 \( (ab)c \) 重新组合为 \( a(bc) \)，两种方式得到的是同一个字母序列；空串满足 \( \lambda + a = a + \lambda = a \)。长度函数与该运算相容，即 \( \lvert a+b \rvert = \lvert a \rvert + \lvert b \rvert \)，这正是连接常被形容为字符串加法的原因。结构 \( A^* \) 称为 \( A \) 上的自由幺半群：形式语言理论的核心对象——语言——就定义为它的子集。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

**不可交换性。** 与数字的加法不同，连接一般依赖于顺序。如果字母表含有多于一个字母，就能找到满足 \( a+b \neq b+a \) 的字符串 \( a \) 和 \( b \)；在字母表 \( \{a, b\} \) 上，字符串 \( ab \) 与 \( ba \) 就已经不同。只有在单字母字母表上，该运算才满足交换律，因为 \( \{a\} \) 上的两个字符串完全由它们的长度决定。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

字母表的选择并没有看上去那么重要。如果两个字母表 \( A_1 \) 和 \( A_2 \) 大小相同，则幺半群 \( A_1^* \) 与 \( A_2^* \) 同构：任何双射 \( f : A_1 \to A_2 \) 都可逐字母延拓为字符串上的双射 \( f^* \)，且保持连接运算，即 \( f^*(a+b) = f^*(a) + f^*(b) \)。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

## 语言的连接

字母表 \( \Sigma \) 上的语言是 \( \Sigma^* \) 的任意子集。连接从字符串逐点提升到语言：\( L_1 \) 与 \( L_2 \) 的连接（也称乘积）为

\[ L_1L_2 = \{ xy : x \in L_1,\ y \in L_2 \}. \]

一个字符串属于 \( L_1L_2 \)，当且仅当它可以拆成取自 \( L_1 \) 的前一部分和取自 \( L_2 \) 的后一部分。例如，取 \( \Sigma = \{a, b, c, 0\} \)、\( L_1 = \{a0, b0, c0\} \)、\( L_2 = \{aa, bb\} \)，则乘积为 \( L_1L_2 = \{a0aa, b0aa, c0aa, a0bb, b0bb, c0bb\} \)，共六个字符串。<sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup><sup> • </sup><sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup>

**幂与闭包。** 语言的幂表示反复与自身连接：\( L^2 = LL \)，\( L^3 = LLL \)，并约定 \( L^0 = \{\varepsilon\} \)、\( L^{n+1} = L^nL \)。由这些幂构造出的两种闭包被频繁使用：<sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup><sup> • </sup><sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup>

- **Kleene 星号** \( L^* = L^0 \cup L^1 \cup L^2 \cup \cdots \) 是把 \( L \) 中任意多个字符串连接起来所能形成的全部字符串的集合，其中包含空串 \( \varepsilon \)。把它施于字母表本身，\( \Sigma^* \) 就是 \( \Sigma \) 上全体字符串的集合，这也是该记号兼作自由幺半群定义的原因。<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup><sup> • </sup><sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup>
- **正闭包** \( L^+ = L^1 \cup L^2 \cup \cdots \) 省略了零次的情形，因此 \( \Sigma^+ = \Sigma^* - \lambda \)。<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup>

同样的星号与加号记号也适用于单个字符串：\( w^* \) 与 \( w^+ \) 分别表示把字符串 \( w \) 重复零次或多次、一次或多次，其中 \( w^0 = \lambda \)。<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup>

连接与其他语言运算以有益的方式相互作用。它对并运算满足分配律，即 \( L(A(1) \vee A(2)) = L(A(1)) \cup L(A(2)) \)，同时 \( L(A(1) \cdot A(2)) = L(A(1)) \cdot L(A(2)) \)；由连接、并、补、交和 Kleene 星号构成的语言方程在这些运算下封闭。此类方程是有限自动机表示的基础；经典方程 \( X = \alpha X + \beta \) 依 Arden 引理有解 \( \alpha^*\beta \)，而当空字不在 \( \alpha \) 中时，这是唯一解。<sup>[4](https://www.ccsc.org/publications/journals/RM2019.pdf)</sup>

## 在正则表达式与自动机中的作用

连接是正则表达式的三个构造子之一，另外两个是并和星号。如果 \( r_1 \) 指称语言 \( L_1 \)，\( r_2 \) 指称语言 \( L_2 \)，则表达式 \( (r_1r_2) \) 指称 \( L_1L_2 \)。<sup>[3](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)</sup> 正则表达式与有限自动机之间的等价性，是通过以连接为黏合剂的构造来证明的：McNaughton-Yamada-Thompson 算法由长度为 \( n \) 的正则表达式构造至多含 \( 2n \) 个状态的 \( \varepsilon \)-NFA；反方向则把 DFA 转换为正则表达式，做法是从一个弧标为正则表达式的广义 NFA 中逐个消去状态；于是从起始状态到接受状态的一条路径，就表示沿途各表达式依次连接的结果。<sup>[5](https://www.cs.columbia.edu/~aho/cs3261/Lectures/L4-Regular_Expressions_and_Finite_Automata.html)</sup>

这一运算也解释了无限语言如何从有限手段中产生。字母表必须是有限的，然而 \( \Sigma^* \) 是无限的，因为长度无界的字符串可以由有限多个符号连接而成；\( \{a, b\} \) 上的语言 \( L = \{a^n b^n : n \geq 0\} \) 就是一个由连接模式定义的无限语言的标准例子。<sup>[2](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)</sup>

## 可数性与计算

尽管 \( A^* \) 是无限的，它却是可数的。当 \( \lvert A \rvert = m \) 且 \( 2^{k-1} < m \leq 2^k \) 时，\( A \) 的每个字母都可编码为二元字母表 \( B = \{0, 1\} \) 上一个长度为 \( k \) 的互不相同的字符串；于是 \( A \) 上的每个字符串都对应一个互不相同的二进制字符串。例如采用编码 \( a \leftrightarrow 000,\ b \leftrightarrow 001,\ c \leftrightarrow 010,\ d \leftrightarrow 011,\ e \leftrightarrow 100 \)，字符串 \( ace \) 就映射为 \( 000010100 \)。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

在编程中，连接是一种熟悉的原语：Python 与 SageMath 用 \( + \) 运算符表示它，因此 `'good'+'bye'` 的求值结果为 `'goodbye'`。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup> 围绕这一运算的形式理论——语言的识别与生成——对应着实际问题：程序员必须生成属于语法正确程序这一语言的字符串，而编译器必须识别给定的字符串是否属于该语言；当这样的识别算法存在时，该语言就称为递归语言。<sup>[1](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)</sup>

## References

1. [13.2: Free Monoids and Languages, Mathematics LibreTexts](https://math.libretexts.org/Courses/SUNY_Schenectady_County_Community_College/Discrete_Structures/13:_Monoids_and_Automata/13.02:_Free_Monoids_and_Languages)
2. [CSci 311, Models of Computation, Chapter 1, University of Mississippi](https://john.cs.olemiss.edu/~hcc/csci311/notes/chap01/ch01.html)
3. [Choo & Taylor, Formal Languages chapter](https://www.maths.usyd.edu.au/u/don/papers/Choo_Taylor.pdf)
4. [Language Equations and Alternating Finite Automata, CCSC Rocky Mountain Conference Proceedings (2019)](https://www.ccsc.org/publications/journals/RM2019.pdf)
5. [L4: Regular Expressions and Finite Automata, Aho, Columbia University CS 3261](https://www.cs.columbia.edu/~aho/cs3261/Lectures/L4-Regular_Expressions_and_Finite_Automata.html)

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

Topic: 物质世界与数学 › 数学与统计学 › 逻辑与离散数学 › General discrete mathematics and discrete structures › Formal languages and automata theory › Formal language fundamentals

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/concatenation

English original: https://www.edgepedia.org/concatenation
