<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Match - 标签 - Victor's Code Journey</title><link>http://www.victorchu.info/tags/match/</link><description>Match - 标签 - Victor's Code Journey</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><managingEditor>victorchu0610@outlook.com (victorchutian)</managingEditor><webMaster>victorchu0610@outlook.com (victorchutian)</webMaster><lastBuildDate>Thu, 29 Nov 2018 16:28:06 +0800</lastBuildDate><atom:link href="http://www.victorchu.info/tags/match/" rel="self" type="application/rss+xml"/><item><title>算法之KMP字符串匹配</title><link>http://www.victorchu.info/posts/2018/11/39af9a85/</link><pubDate>Thu, 29 Nov 2018 16:28:06 +0800</pubDate><author><name>victorchutian</name></author><guid>http://www.victorchu.info/posts/2018/11/39af9a85/</guid><description><![CDATA[<div class="featured-image">
                <img src="/feature-images/algorithm.webp" referrerpolicy="no-referrer">
            </div><p>有一个文本串S，和一个模式串P，现在要查找P在S中的位置，怎么查找呢？</p>
<p>假设主串target为: <code>a b a c a a b a c a b a c a b a a b b</code>,模式串pattern: <code>a b a c a b</code>(为了方便查看，每个字符间用空格隔开)。</p>
<p>用暴力算法匹配字符串过程中，我们会把<code>target[0]</code>跟 <code>pattern[0]</code> 匹配，如果相同则匹配下一个字符，直到出现不相同的情况，此时我们会丢弃前面的匹配信息，然后把<code>target[1]</code>跟 <code>pattern[0]</code> 匹配，循环进行，直到主串结束，或者出现匹配成功的情况。这种丢弃前面的匹配信息的方法，极大地降低了匹配效率。</p>
<p>以上面的字符为例子:pattern的前5个字符<code>abaca</code>可以匹配target的前5个字符,但是<code>pattern[5]</code>和<code>target[5]</code>不匹配。下面重新从target[1]开始和pattern匹配。</p>
<p>显然效率很差，因为你要把&quot;搜索位置&quot;移到已经比较过的位置，重比一遍。</p>]]></description></item></channel></rss>