跳到正文
产品桌面工具TASTECAD图纸查看TASTEFILES文件管理TASTEFTP文件传输TASTESSH远程工作空间TASTEZIP压缩与解压TASTEWINM文件管理开发工具TASTECODERust 集成开发环境TASTESTUDIO集成开发与测试环境TASTEETL数据整合(ETL)服务器TASTEREMOTE远程桌面服务器TASTEGEO空间信息服务器TASTEMAIL邮件工作空间投资TASTESTOCK股票自动交易桌面应用开始使用下载文档购买全部产品
市场
资讯
技术资讯今日开发资讯开发文档编程语言RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express应用与桌面框架TauriFlutterReact NativeElectron算法算法基础核心算法进阶算法实用算法
社区项目开发日志提示词AI 工具问题作品展示社区指南
合作技术传授网站构建桌面应用构建发布与支付流水线销售合作伙伴联系我们
服务远程桌面安装包定制
TASTEDEV
产品

桌面工具

TASTECAD复杂图纸,轻松打开TASTEFILES只要需要的文件,最快送达TASTEFTP让文件往来,干净利落TASTESSH你与服务器之间,连接更牢靠TASTEZIP压得更快,解得更稳TASTEWINM以键盘为主、用两个窗格整理文件的文件管理器

开发工具

TASTECODE在一个窗口中完成 Rust 应用的编辑、构建、调试和测试TASTESTUDIO从编辑到远程测试,再到经批准的 AI 修复TASTEETL边看数值,边搬数据

服务器

TASTEREMOTE在自己的服务器上运行的远程桌面TASTEGEO用 Rust 重新构建兼容 GeoServer 的空间信息服务器TASTEMAIL把邮件的主导权拿回自己手中

投资

TASTESTOCK何时、做什么——按你定下的规则运行的股票自动交易

开始使用

下载所有产品的最新版本文档各产品使用方法购买订阅与永久许可全部产品一览全部产品
全部产品
市场资讯
技术

资讯

今日开发资讯语言、框架、GitHub 与 AI 官方发布开发文档语言、框架与算法指南

编程语言

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

框架

Web 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular
服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express
应用与桌面框架TauriFlutterReact NativeElectron

算法

算法基础复杂度分析 · 排序 · 查找 · 基础数据结构 · 树与堆核心算法图遍历 · 最短路径与最小生成树 · 动态规划 · 贪心算法 · 分治法 · 回溯法进阶算法字符串算法 · 数学与数论 · 位运算 · 线段树 · 并查集实用算法编程面试解题策略 · LRU 缓存 · 限流 · 一致性哈希
查看全部开发文档
社区
项目发现开发者与作品开发日志决策与试错提示词可复用的指令AI 工具问题Claude、Codex、Gemini 的问题与变通办法作品展示已完成的成果社区指南我们如何公开构建
合作
技术传授用 Rust 和 egui 打造原生应用网站构建10 种语言、账户、文档与下载桌面应用构建Rust 和 egui,不用 WebView发布与支付流水线签名、MSI、deb、rpm、AppImage、哈希销售合作伙伴推荐即得 30%联系我们开发、合作与投资咨询
服务
远程桌面用浏览器连接你的电脑安装包定制只装需要的,设置预先放好
购买
ZH
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
登录
登录
TASTEDEV

从小工具到更好的开发。

support@tastedev.net支持 TASTEDEV
产品产品线购买文档下载市场客户支持
社区项目开发日志提示词AI 工具问题作品展示
政策关于我们隐私政策服务条款订阅条款退款政策社区规范
账户登录注册
访客
今天
昨天
累计浏览
公司名称
GXSOFT
法定代表人
CHOI YOUNG GU
营业执照号
853-44-00368
地址
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
电话
+82-10-4404-1523
电子邮件
whitecalvin@tastedev.net
个人信息保护负责人
CHOI YOUNG GU

© 2026 TASTEDEV产品信息以已验证的开发源码为准。已发布·持续改进

字符串算法 文档
字符串算法
ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
讨论

字符串算法 指南 · 2/6

How It Works

本章目前仅提供英文版。

This chapter traces KMP, Rabin-Karp, tries and the Z-algorithm by hand on small inputs. Working out the tables yourself makes the code in the next chapter much easier to read.

KMP step 1: the prefix function (failure table)

The prefix function pi[i] is the length of the longest border of the first i + 1 characters of the pattern (p[0..i]). It is also called the failure function or failure table. Here it is for ababaca.

i0123456
p[i]ababaca
pi[i]0012301

The computation runs left to right and carries k, the border length at the previous position.

  1. i = 1: p[1] = b differs from p[0] = a and k = 0, so pi[1] = 0.
  2. i = 2: p[2] = a equals p[0], so k = 1 and pi[2] = 1.
  3. i = 3, 4: the characters keep matching and k grows to 2, then 3.
  4. i = 5: p[5] = c differs from p[3] = b. Instead of starting over, fall back to a shorter border: k = pi[k - 1] = pi[2] = 1. p[1] = b still differs, so k = pi[0] = 0; p[0] = a differs too, and pi[5] = 0.
  5. i = 6: p[6] = a equals p[0], so pi[6] = 1.

Step 4 is the key idea. On a mismatch you jump straight to the next shorter border inside the current one. Because a border of a border is itself a border, no candidate is skipped.

def prefix_function(p: str) -> list[int]:
    pi = [0] * len(p)
    k = 0
    for i in range(1, len(p)):
        while k > 0 and p[i] != p[k]:
            k = pi[k - 1]
        if p[i] == p[k]:
            k += 1
        pi[i] = k
    return pi


print(prefix_function("ababaca"))  # [0, 0, 1, 2, 3, 0, 1]

KMP step 2: scanning the text

Searching works the same way. k is the number of pattern characters matched so far, and the text pointer i never moves backwards. Search for () in :

对本章有疑问?
上一章Concepts下一章Implementation
abab
pi = [0, 0, 1, 2]
abababcabab
iCharActionk afterResult
0-3a b a ball match4match at 0, k = pi[3] = 2
4amatches p[2] = a3
5bmatches p[3] = b4match at 2, k = 2
6cfails against p[2], then p[0]0
7-10a b a ball match4match at 7

Setting k = pi[m - 1] after a match, instead of 0, is what finds overlapping occurrences such as the ones at 0 and 2.

Rabin-Karp: the rolling hash

Rabin-Karp compares the hash of each length-m window with the hash of the pattern. When the window slides by one, the hash is not recomputed from scratch: the leading character is removed and the new one added in O(1).

h(s) = (s[0]·B^(m-1) + s[1]·B^(m-2) + ... + s[m-1]) mod M
next window: h' = ((h - s[i]·B^(m-1))·B + s[i+m]) mod M

To keep the numbers readable, use a digit string with B = 10 and M = 11. The text is 31415926, the pattern is 1592, and the pattern hash is 1592 mod 11 = 8.

StartWindowHashVerdict
031416skip
114157skip
241591skip
315928characters compared: match
459268characters compared: no match (false positive)

As the last row shows, equal hashes do not guarantee equal strings, so a hash hit must always be confirmed by comparing characters. Real code uses a large prime modulus such as 10**9 + 7 and a random base to make collisions rare.

def window_hashes(text: str, m: int, base: int = 10, mod: int = 11) -> list[int]:
    high = pow(base, m - 1, mod)
    h = 0
    for ch in text[:m]:
        h = (h * base + int(ch)) % mod
    out = [h]
    for i in range(len(text) - m):
        h = ((h - int(text[i]) * high) * base + int(text[i + m])) % mod
        out.append(h)
    return out


print(window_hashes("31415926", 4))  # [6, 7, 1, 8, 8]

Tries: a tree of shared prefixes

A trie is a tree you walk down one character at a time. After inserting car, cart, cat and dog, the common prefix ca is stored once. A * marks a node where a word ends.

(root)
 ├─ c ─ a ─ r* ─ t*
 │        └─ t*
 └─ d ─ o ─ g*

To autocomplete ca, walk down c then a and collect every end-of-word mark below that node: car, cart, cat. The walk costs time proportional to the prefix length, independent of how many words the dictionary holds.

The Z-algorithm in brief

In the Z-array, z[i] is the length of the longest common prefix of s[i:] and s. For aabxaab it is [7, 1, 0, 0, 3, 1, 0] (by convention z[0] is the length or 0). To search, build the Z-array of pattern + "$" + text and report positions where the value equals m; the separator $ must not appear in either string. By reusing the rightmost interval [l, r) already matched (the Z-box), the whole array is computed in O(n + m).

Summary

  • On a mismatch the prefix function falls back to a border of a border, so the text pointer never moves back.
  • After a match KMP sets k = pi[m - 1], which also finds overlapping occurrences.
  • Rabin-Karp updates each window hash in O(1) and confirms hash hits character by character to reject false positives.
  • Tries share common prefixes; the Z-array solves the same problems as KMP from a different angle.

0 条评论

热门评论最新优先

登录 · 登录后即可发表评论。