# @algorithm\.ts/lcs 中文文档

规范地址：https://npmcn.com/package/%40algorithm.ts/lcs/

版本：4\.0\.6
npm 元数据更新时间：2026-03-23T11:39:13.849Z
中文译文更新时间：2026-11-05T16:56:56.940Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40algorithm.ts/lcs
原文对照：https://npmcn.com/package/%40algorithm.ts/lcs/?lang=original
项目主页：https://github.com/guanghechen/algorithm.ts/tree/@algorithm.ts/lcs@4.0.5/packages/lcs#readme
源码仓库：https://github.com/guanghechen/algorithm.ts#@algorithm.ts

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：61.1 kB，7 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2022-02-22T13:15:56.828Z
- 最新版本发布：2026-03-23T11:39:13.688Z
- 近 30 天下载量：3052267（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：36（近 12 个月 1 个）

## 中文简介

寻找最长的普通子序列.

## 安装

```sh
npm install @algorithm.ts/lcs
```

也可以使用：`pnpm add @algorithm.ts/lcs`、`yarn add @algorithm.ts/lcs`、`bun add @algorithm.ts/lcs`

## README

# [@algoritm.ts/ lcs (英语)](https://github.com/guanghechen/algorithm.ts/tree/@algorithm.ts/lcs@4.0.5/packages/lcs#readme)

 [![Npm Version](https://img.shields.io/npm/v/@algorithm.ts/lcs.svg)](https://www.npmjs.com/package/@algorithm.ts/lcs)[![Npm Download](https://img.shields.io/npm/dm/@algorithm.ts/lcs.svg) ](https://www.npmjs.com/package/@algorithm.ts/lcs)[![Npm License](https://img.shields.io/npm/l/@algorithm.ts/lcs.svg) ](https://www.npmjs.com/package/@algorithm.ts/lcs)[![Module Formats: cjs, esm](https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg) ](https://npmcn.com/package/%40algorithm.ts/lcs/#readme-install)[![Node.js Version](https://img.shields.io/node/v/@algorithm.ts/lcs) ](https://github.com/nodejs/node)[![Tested with Jest](https://img.shields.io/badge/tested_with-vitest-6E9F18.svg) ](https://github.com/vitest-dev/vitest)[![Code Style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

  

查找“% 1” 的打印符执行 **分类** (长相通分序.

这个软件包提供了Lcs算法的三个不同的执行. 衡量复杂程度 这些算法,让$N 1美元和$N 2美元分别成为两个序列的子序列长度. 而让$L$成为最长的普通子序列的长度,再由$D=2L-N 1-N 2$.

1.  `myers_lcs(N1: number, N2: number, equals: (x: number, y: number) => boolean): [x: number, y: number][]`数字 : 本文介绍的香草算法 [_O(ND) 差异算法及其变化_](https://mailserver.org/diff2.pdf)。 。 。 .
    
    -   时间复杂度: $O( (N 1 + N 2))\\times D)$
    -   内存复杂度: $O( N 1)\\ times N 2) $
2.  `myers_lcs_linear_space(N1: number, N2: number, equals: (x: number, y: number) => boolean): [x: number, y: number][]`数字 : 线性空间精密算法 [_O(ND) 差异算法及其变化_](https://mailserver.org/diff2.pdf)。 。 。 .
    
    -   时间复杂度: $O(N 1 + N 2) \* D) 美元
    -   内存复杂度:$O(N 1 + N 2)
3.  `lcs_dp(N1: number, N2: number, equals: (x: number, y: number) => boolean): [x: number, y: number][]` 这种执行基于动态编程,可以找到最小的词典 医生.
    
    -   时间复杂度: $O( N 1)\\ times N 2) $
    -   内存复杂度: $O( N 1)\\ times N 2) $

以下定义引自维基百科 (单位:千美元)[https://en.wikipedia.org/wiki/Longest common subsequence problem 互联网档案馆的存檔,存档日期2014-12-21](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem):

> 最长的普通子序列(LCS)问题是找到最长的子序列的问题 常见于一组序列中的所有序列(通常只有两个序列). 和那个不同 最长的常见子字符串问题:与子字符串不同,子序列不需要占用 原序列内的连续位置。 最长的普通子序列问题是 经典的计算机科学问题, 数据比较程序的基础 如 diff 工具, 并具有计算语言学和生物信息学的应用. 它也被广泛使用 修改控制系统,如用于调和对a的多处更改的 Git 修订控制的文件集.

## 安装

-   无
    
    ```bash
    npm install --save @algorithm.ts/lcs
    ```
    
-   小时
    
    ```bash
    pnpm add @algorithm.ts/lcs
    ```
    

## 使用量

-   基础
    
    ```typescript
    import { lcs_dp, lcs_myers_size } from '@algorithm.ts/lcs'
    
    const s1: number[] = [1, 2, 3, 4, 6, 6, 7, 8, 6]
    const s2: number[] = [2, 3, 4, 7, 9, 8, 2, 3, 5, 2]
    
    lcs_myers_size(s1.length, s2.length, (x, y) => s1[x] === s2[y]) // => 5
    
    lcs_dp(s1.length, s2.length, (x, y) => s1[x] === s2[y])
    // => [ [1, 0], [2, 1], [3, 2], [6, 3], [7, 5] ]
    //
    //    Here is why:
    //
    //          0 1 2 3 4 5 6 7 8 9
    //      s1: 1 2 3 4 6 6 7 8 6
    //      s2: 2 3 4 7 9 8 2 3 5 2
    //
    //      s1[1] <----> s2[0]
    //      s1[2] <----> s2[1]
    //      s1[3] <----> s2[2]
    //      s1[6] <----> s2[3]
    //      null  <----> s2[4]
    //      s1[7] <----> s2[5]
    //      null  <----> s2[6]
    //      null  <----> s2[7]
    //      null  <----> s2[8]
    //      null  <----> s2[9]
    ```
    

## 相关

-   [O(ND) 差异算法及其变化](https://mailserver.org/diff2.pdf)。 。 。 .
-   [最长公共子序列(LCS)| (光和尘)](https://me.guanghechen.com/post/algorithm/lcs/)
-   [维基百科中的相关条目: 相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)

## 常见问题

### @algorithm\.ts/lcs 是什么？

寻找最长的普通子序列\.

### 如何安装 @algorithm\.ts/lcs？

在项目目录运行 npm install @algorithm\.ts/lcs，或使用 pnpm add / yarn add / bun add。

### @algorithm\.ts/lcs 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @algorithm\.ts/lcs 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @algorithm\.ts/lcs 可以商用吗？

@algorithm\.ts/lcs 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @algorithm\.ts/lcs 的下载量有多少？

根据 npm 官方下载统计，@algorithm\.ts/lcs 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,052,267 次，日均约 101,742 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @algorithm\.ts/lcs 还在维护吗？

最新版本 4\.0\.6 发布于 2026/03/23（7 个月前）；至今共发布 36 个版本，其中过去 12 个月发布了 1 个；首次发布于 2022/02/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

