{"format":"brushup-mindmap-v1","id":"ds-04-string","title":"串【王道】","structure":"logic-right","theme":"auto","root":{"id":"8088b6ce26697ed1b30c991ae0","topic":"第四章 串","expanded":true,"children":[{"id":"47dba712-cf12-486d-a564-ac4eefb81850","topic":"串的定义与实现","expanded":true,"children":[{"id":"ad6a8758-21b2-4865-b60a-a32b5223dabb","topic":"概念","expanded":true,"children":[{"id":"1b3151a9-6cf3-43df-a3f6-4c15f264d4a7","topic":"串的概念","expanded":true,"children":[{"id":"05341d65-3160-4f90-a607-b4a3dbd5e5cf","topic":"零个或多个任意字符组成的有限序列（内容受限的线性表）","expanded":true}]},{"id":"70691a75-baca-4e86-b0a9-c1dd96843f83","topic":"子串的概念","expanded":true,"children":[{"id":"aefdf35a-ef85-412b-a0b1-52ce52b914fe","topic":"一个串中任意个连续字符组成的子序列（含空串）","expanded":true}]}]},{"id":"74097e2e6c2a4988afc90a3d40","topic":"串的存储结构（和线性表几乎一样，仅将存储数据的类型改为字符）","expanded":true,"children":[{"id":"8644ebea-3349-4213-a9e6-4b860fb9bb86","topic":"顺序存储结构","expanded":true},{"id":"a75207df-53aa-47af-be1f-e8aafe789f7f","topic":"链式存储结构","expanded":true,"children":[{"id":"1de775b9-091b-44f8-b010-749581061d33","topic":"一个结点若只存一个字符，存储密度过低，引出块链存储结构","expanded":true}]},{"id":"42227745-7c89-4c8e-b11f-f20e71fc2248","topic":"块链存储结构","expanded":true,"children":[{"id":"2b287eb1-0aff-4472-83ed-356a8ddf7dc6","topic":"每个结点既可以存放一个字符，也可以存放多个字符。每个结点称为块","expanded":true}]}]}]},{"id":"9adbbca47c8b4996014d18159e","topic":"串的模式匹配","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"220b938a-63a3-47b2-9316-bcb587720407","topic":"简单模式匹配算法","expanded":true,"children":[{"id":"081995b4-70a9-41ae-9157-030ea83c0190","topic":"概念","expanded":true,"children":[{"id":"a0973a35-4302-495e-9a4d-dda7d9290249","topic":"子串的定位操作通常称为串的模式匹配，它求的是子串（模式串）在主串中的位置","expanded":true}]},{"id":"d7d13ec1-e78f-4a70-a03d-c74816621d08","topic":"算法思想","expanded":true,"children":[{"id":"0956f745-7361-4850-b0f5-897345ec9dfd","topic":"将主串中与模式串长度相同的子串逐个与模式串匹配，暴力法","expanded":true}]},{"id":"3c32ff7b-6a47-4768-8068-dd6e3ed0e3b8","topic":"缺点","expanded":true,"children":[{"id":"b8a5adff-d2d8-42db-9fea-2154d4760857","topic":"主串指针回溯导致时间开销，时间复杂度为：O(nm)，nm分别为主串和模式串的长度","expanded":true}]}]},{"id":"c16da628-57a3-49e6-9706-6f329980c0a9","topic":"KMP算法","expanded":true,"children":[{"id":"4200d5b9-9fcb-4716-8806-fec565e74d68","topic":"基本概念","expanded":true,"children":[{"id":"c0749ef6-50bf-4427-83db-61dc97d09f1a","topic":"前缀","expanded":true,"children":[{"id":"70285873-67e5-4748-a615-a2c21109a5c7","topic":"除最后一个字符外，字符串的所有头部子串","expanded":true}]},{"id":"fd77d20c-127a-49d3-9986-329f5631a952","topic":"后缀","expanded":true,"children":[{"id":"f4798b35-287b-4c5f-a62e-3e1bd0554fa9","topic":"除第一个字符外，字符串的所有尾部子串","expanded":true}]}]},{"id":"7533ea0d-eebb-4938-94ad-1825512b42c2","topic":"算法思想","expanded":true,"children":[{"id":"fb33413a-1a02-494b-badf-4aa0aab657eb","topic":"主串指针不必回溯，模式串指针根据next数组部分回溯（如果已匹配相等的前缀序列中有某个后缀正好是模式的前缀，可以将模式串向后滑动到与这些相等字符对齐的位置）","expanded":true}]},{"id":"b06d4913-49aa-4511-bfe0-1a9031b8d384","topic":"next数组","expanded":true,"children":[{"id":"95df4214-5536-4e3f-9ea6-a5ef09b52e65","topic":"表明当模式中第j个字符与主串中相应字符匹配失败时，在模式中需重新和主串中该字符进行比较的字符位置","expanded":true,"children":[{"id":"49a68a90-ccf2-4eeb-ae65-df47fd0e9a8d","topic":"即衡量模式串向后滑动的量","expanded":true}]},{"id":"8ed41f6b-4534-4eec-8313-8c5c3935f25a","topic":"计算","expanded":true,"children":[{"id":"e339b3bc-5a0d-4ac3-a9f1-48961e3c573d","topic":"next[j] 公式","expanded":true,"children":[{"id":"746575e7-9d1a-476a-8865-2d0ef67bd06e","topic":"以“abab”为例","expanded":true,"children":[{"id":"9e861ebe-4162-434b-a1a4-350688600ecf","topic":"序号j = 1时，属于j == 1情况，next[j] = 0","expanded":true},{"id":"a1fe3456-72a2-413a-967d-2e1c5333e003","topic":"序号j = 2时，‘a’的前后缀都为空集，属于其他情况，next[j] = 1","expanded":true},{"id":"f7cfdb59-71a2-419a-9384-a86f08068496","topic":"序号j = 3时，‘ab’的前缀为{a}，后缀为{b}，交集为空，属于其他情况，next[j] = 1","expanded":true},{"id":"43489c2b-325a-4a74-8944-079912b06c24","topic":"序号j = 4时，‘aba’的前缀为{a,ab}，后缀为{a,ba}，交集为{a}，k - 1 = 1（长度），next[j] = k = 2（长度+1）","expanded":true}]}],"formula":{"tex":"\\mathbf{next}[j] = \\begin{cases} \\max\\{ k \\mid 1 < k < j,\\text{ 且 “}\\overbrace{p_1\\dots p_{k-1}}^{\\text{从头开始的 }k-1\\text{ 个元素}}\\text{”} = \\text{“}\\overbrace{p_{j-k+1}\\dots p_{j-1}}^{\\text{j前面的}k-1\\text{个元素}}\\text{”} \\}, \\\\ \\quad\\text{当此集合非空时} \\\\ 0, \\quad\\text{当 } j = 1 \\text{ 时} \\\\ 1, \\quad\\text{其他情况} \\end{cases}","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mrow><mi mathvariant=\"bold\">n</mi><mi mathvariant=\"bold\">e</mi><mi mathvariant=\"bold\">x</mi><mi mathvariant=\"bold\">t</mi></mrow><mo stretchy=\"false\">[</mo><mi>j</mi><mo stretchy=\"false\">]</mo><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>max</mi><mo>⁡</mo><mo stretchy=\"false\">{</mo><mi>k</mi><mo>∣</mo><mn>1</mn><mo>&lt;</mo><mi>k</mi><mo>&lt;</mo><mi>j</mi><mo separator=\"true\">,</mo><mtext> 且 “</mtext><mover><mover><mrow><msub><mi>p</mi><mn>1</mn></msub><mo>…</mo><msub><mi>p</mi><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></msub></mrow><mo stretchy=\"true\">⏞</mo></mover><mrow><mtext>从头开始的 </mtext><mi>k</mi><mo>−</mo><mn>1</mn><mtext> 个元素</mtext></mrow></mover><mtext>”</mtext><mo>=</mo><mtext>“</mtext><mover><mover><mrow><msub><mi>p</mi><mrow><mi>j</mi><mo>−</mo><mi>k</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>…</mo><msub><mi>p</mi><mrow><mi>j</mi><mo>−</mo><mn>1</mn></mrow></msub></mrow><mo stretchy=\"true\">⏞</mo></mover><mrow><mtext>j前面的</mtext><mi>k</mi><mo>−</mo><mn>1</mn><mtext>个元素</mtext></mrow></mover><mtext>”</mtext><mo stretchy=\"false\">}</mo><mo separator=\"true\">,</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mspace width=\"1em\"/><mtext>当此集合非空时</mtext></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mn>0</mn><mo separator=\"true\">,</mo><mspace width=\"1em\"/><mtext>当 </mtext><mi>j</mi><mo>=</mo><mn>1</mn><mtext> 时</mtext></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mn>1</mn><mo separator=\"true\">,</mo><mspace width=\"1em\"/><mtext>其他情况</mtext></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">\\mathbf{next}[j] = \\begin{cases} \\max\\{ k \\mid 1 &lt; k &lt; j,\\text{ 且 “}\\overbrace{p_1\\dots p_{k-1}}^{\\text{从头开始的 }k-1\\text{ 个元素}}\\text{”} = \\text{“}\\overbrace{p_{j-k+1}\\dots p_{j-1}}^{\\text{j前面的}k-1\\text{个元素}}\\text{”} \\}, \\\\ \\quad\\text{当此集合非空时} \\\\ 0, \\quad\\text{当 } j = 1 \\text{ 时} \\\\ 1, \\quad\\text{其他情况} \\end{cases}</annotation></semantics></math></span>"}}]}]},{"id":"5b6147a1-253c-4a9a-8069-dd21ab7f924e","topic":"时间复杂度：O(m + n)","expanded":true}]},{"id":"01044c61-1ea6-4728-8e30-d689f88c6352","topic":"进一步优化","expanded":true,"children":[{"id":"c09ec9bc-3a12-4513-8e9b-a115dd4b1f58","topic":"构造nextval数组","expanded":true,"children":[{"id":"3e6bf390-a31f-44d6-82c0-440bd16474a1","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-0e739b355d557c.png","width":628,"height":440},"children":[{"id":"0f9136da-3fa2-4666-9211-fe4d1abf410f","topic":"若模式串指向的位置的字符等于本身，可以直接赋值","expanded":true},{"id":"374f0fd4-e41d-4253-a193-5ee50a6f5336","topic":"举例","expanded":true,"children":[{"id":"bba2ad6b-d9d6-4635-ab62-158cc8f24ae9","topic":"5号的a中next数组指向3号位置，3号位置也是a故直接把3号的next数组值复制过去","expanded":true},{"id":"644b5880-620e-4619-a6fe-4bf2deeca483","topic":"6号的a中next数组指向4号位置，两者字符不同，不能优化","expanded":true}]}]}]}]}]},{"id":"14b13e6e-a110-4713-ae29-98fc92953670","topic":"代码","expanded":true,"children":[{"id":"db8a48c7-a2d8-4e5c-8818-2a53479ed88e","topic":"王道代码见教材","expanded":true},{"id":"619bfc5c-749e-4696-a60d-4297dcc071d0","topic":"补充代码","expanded":true,"children":[{"id":"9ef74a86-478d-4167-bd32-23b19541d2ad","topic":"cin >> n >> p+1 >> m >> s+1;// char数组s是长文本，p是模式串，且从数组下标1开始存储\nfor(int i=2,j=0;i<=n;i++){  //求next数组\n    while(j&&p[i]!=p[j+1]) j=ne[j];\n    if(p[i]==p[j+1]) j++;\n    ne[i]=j;\n}\nfor(int i=1,j=0;i<=m;i++){  //匹配\n    while(j&&s[i]!=p[j+1]) j=ne[j];\n    if(s[i]==p[j+1]) j++;\n    if(j==n){\n        j=ne[j];\n        //匹配成功\n    }\n}","expanded":true}]}]}]}}