<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm</id>
		<title>Knuth–Morris–Pratt algorithm - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm"/>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;action=history"/>
		<updated>2026-04-24T09:16:32Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1480&amp;oldid=prev</id>
		<title>Brian: /* Computation of the prefix function */ --- whoops. battlefield test just now failed</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1480&amp;oldid=prev"/>
				<updated>2011-12-11T22:01:58Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Computation of the prefix function: &lt;/span&gt; --- whoops. battlefield test just now failed&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 22:01, 11 December 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L37&quot; &gt;Line 37:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 37:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; S[i]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; S[i]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; if k = &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;0&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; if &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S[&lt;/ins&gt;k&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;+1] &lt;/ins&gt;= &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S[i]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; 0&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; 0&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; else&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; else&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L50&quot; &gt;Line 50:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 50:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; S[i]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; S[i]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; if k &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;amp;ne; 0&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; if &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S[&lt;/ins&gt;k&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;+1] = S[i]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; k+1&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; k+1&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; k&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; k&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1428&amp;oldid=prev</id>
		<title>Brian: /* Example 1 */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1428&amp;oldid=prev"/>
				<updated>2011-10-23T08:06:04Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Example 1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 08:06, 23 October 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L5&quot; &gt;Line 5:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 5:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Example 1===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Example 1===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In this example, we are searching for the string &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; = '''aaa''' in the string &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; = '''aaaaaaaaa''' (in which it occurs seven times). The naive algorithm would begin by comparing &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and thus find a match for &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 1 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Then it would proceed to compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_4&amp;lt;/math&amp;gt;, and thus find a match at position 2 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and so on, until it finds all the matches. But we can do better than this, if we preprocess &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and note that &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; are the same, and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; are the same. That is, the '''prefix''' of length 2 in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; matches the '''substring''' of length 2 starting at position 2 in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;; ''&amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; partially matches itself''. Now, after finding that &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_1, T_2, T_3&amp;lt;/math&amp;gt;, respectively, we no longer care about &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, since we are trying to find a match at position 2 now, but we still know that &amp;lt;math&amp;gt;S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3&amp;lt;/math&amp;gt; respectively. Since we already know &amp;lt;math&amp;gt;S_1 = S_2, S_2 = S_3&amp;lt;/math&amp;gt;, we now know that &amp;lt;math&amp;gt;S_1, S_2&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3&amp;lt;/math&amp;gt; respectively; there is no need to examine &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt; again, as the naive algorithm would do. If we now check that &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;T_4&amp;lt;/math&amp;gt;, then, after finding &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 1 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, we only need to do ''one'' more comparison (not three) to conclude that &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; also occurs at position 2 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. So now we know that &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3, T_4&amp;lt;/math&amp;gt;, respectively, which allows us to conclude that &amp;lt;math&amp;gt;S_1, S_2&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_3, T_4&amp;lt;/math&amp;gt;. Then we compare &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_5&amp;lt;/math&amp;gt;, and find another match, and so on. Whereas the naive algorithm needs three comparisons to find each occurrence of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, our technique only needs three comparisons to find the ''first'' occurrence, and only one for each after that, and doesn't go back to examine previous characters of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; again. (This is how a human would probably do this search, too.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;In this example, we are searching for the string &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; = '''aaa''' in the string &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; = '''aaaaaaaaa''' (in which it occurs seven times). The naive algorithm would begin by comparing &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and thus find a match for &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 1 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Then it would proceed to compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_4&amp;lt;/math&amp;gt;, and thus find a match at position 2 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and so on, until it finds all the matches. But we can do better than this, if we preprocess &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and note that &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; are the same, and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; are the same. That is, the '''prefix''' of length 2 in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; matches the '''substring''' of length 2 starting at position 2 in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;; ''&amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; partially matches itself''. Now, after finding that &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_1, T_2, T_3&amp;lt;/math&amp;gt;, respectively, we no longer care about &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, since we are trying to find a match at position 2 now, but we still know that &amp;lt;math&amp;gt;S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3&amp;lt;/math&amp;gt; respectively. Since we already know &amp;lt;math&amp;gt;S_1 = S_2, S_2 = S_3&amp;lt;/math&amp;gt;, we now know that &amp;lt;math&amp;gt;S_1, S_2&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3&amp;lt;/math&amp;gt; respectively; there is no need to examine &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt; again, as the naive algorithm would do. If we now check that &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;T_4&amp;lt;/math&amp;gt;, then, after finding &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 1 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, we only need to do ''one'' more comparison (not three) to conclude that &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; also occurs at position 2 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. So now we know that &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_2, T_3, T_4&amp;lt;/math&amp;gt;, respectively, which allows us to conclude that &amp;lt;math&amp;gt;S_1, S_2&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_3, T_4&amp;lt;/math&amp;gt;. Then we compare &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_5&amp;lt;/math&amp;gt;, and find another match, and so on. Whereas the naive algorithm needs three comparisons to find each occurrence of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, our technique only needs three comparisons to find the ''first'' occurrence, and only one for each after that, and doesn't go back to examine previous characters of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; again. (This is how a human would probably do this search, too.&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Example 2===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Example 2===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now let's search for the string &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; = '''aaa''' in the string &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; = '''aabaabaaa'''. Again, we start out the same way as in the naive algorithm, hence, we compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;. Here we find a mismatch between &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; does ''not'' occur at position 1 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Now, the naive algorithm would continue by comparing &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and would find a mismatch; then it would compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and find a mismatch, and so on. But a human would notice that after the first mismatch, the possibilities of finding &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at positions 2 and 3 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; are extinguished. This is because, as noted in Example 1, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; is the same as &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, and since &amp;lt;math&amp;gt;S_3 \neq T_3&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2 \neq T_3&amp;lt;/math&amp;gt; also (so we will not find &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 2 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;). And, likewise, since &amp;lt;math&amp;gt;S_1 \neq S_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_2 \neq T_3&amp;lt;/math&amp;gt;, it is also true that &amp;lt;math&amp;gt;S_1 \neq T_3&amp;lt;/math&amp;gt;, so it is pointless looking for a match at the third position of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Thus, it would make sense to start comparing again at the fourth position of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; (''i.e.'', &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_4, T_5, T_6&amp;lt;/math&amp;gt;, respectively). Again finding a mismatch, we use similar reasoning to rule out the fifth and sixth positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and begin matching again at &amp;lt;math&amp;gt;T_7&amp;lt;/math&amp;gt; (where we finally find a match.) Again, notice that the characters of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; were examined strictly in order.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Now let's search for the string &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; = '''aaa''' in the string &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; = '''aabaabaaa'''. Again, we start out the same way as in the naive algorithm, hence, we compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_1&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;. Here we find a mismatch between &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; does ''not'' occur at position 1 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Now, the naive algorithm would continue by comparing &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_2&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and would find a mismatch; then it would compare &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_3&amp;lt;/math&amp;gt;, and find a mismatch, and so on. But a human would notice that after the first mismatch, the possibilities of finding &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at positions 2 and 3 in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; are extinguished. This is because, as noted in Example 1, &amp;lt;math&amp;gt;S_2&amp;lt;/math&amp;gt; is the same as &amp;lt;math&amp;gt;S_3&amp;lt;/math&amp;gt;, and since &amp;lt;math&amp;gt;S_3 \neq T_3&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;S_2 \neq T_3&amp;lt;/math&amp;gt; also (so we will not find &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position 2 of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;). And, likewise, since &amp;lt;math&amp;gt;S_1 \neq S_2&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;S_2 \neq T_3&amp;lt;/math&amp;gt;, it is also true that &amp;lt;math&amp;gt;S_1 \neq T_3&amp;lt;/math&amp;gt;, so it is pointless looking for a match at the third position of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. Thus, it would make sense to start comparing again at the fourth position of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; (''i.e.'', &amp;lt;math&amp;gt;S_1, S_2, S_3&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_4, T_5, T_6&amp;lt;/math&amp;gt;, respectively). Again finding a mismatch, we use similar reasoning to rule out the fifth and sixth positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and begin matching again at &amp;lt;math&amp;gt;T_7&amp;lt;/math&amp;gt; (where we finally find a match.) Again, notice that the characters of &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; were examined strictly in order.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1316&amp;oldid=prev</id>
		<title>Brian at 02:47, 7 April 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1316&amp;oldid=prev"/>
				<updated>2011-04-07T02:47:13Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 02:47, 7 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L95&quot; &gt;Line 95:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 95:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==External links==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* {{SPOJ|NHAY|A Needle in the Haystack}}&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1308&amp;oldid=prev</id>
		<title>Brian at 05:19, 6 April 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1308&amp;oldid=prev"/>
				<updated>2011-04-06T05:19:32Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 05:19, 6 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L30&quot; &gt;Line 30:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 30:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;amp;pi;[1] &amp;amp;larr; 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;for i &amp;amp;isin; [2..m]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[i-1]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; S[i]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; if k = 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; else&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;amp;pi;[i] &amp;amp;larr; k+1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;With a little bit of thought, this can be re-written as follows:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;amp;pi;[1] &amp;amp;larr; 0&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;amp;pi;[1] &amp;amp;larr; 0&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L49&quot; &gt;Line 49:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 62:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Think carefully about what this means. If &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; does not satisfy the statement that &amp;lt;math&amp;gt;S_1, \ldots, S_{k-p}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+p}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, then the needle &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''does not match'' &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, ''i.e.'', we can ''rule out'' the position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;. On the other hand, if &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; ''does'' satisfy this statement, then &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''might'' match &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, and, in fact, all the characters up to but not including &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt; have already been verified to match the corresponding characters in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, so we can proceed by comparing &amp;lt;math&amp;gt;S_{k-p+1}&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;, and, as promised, ''never need to look back''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Think carefully about what this means. If &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; does not satisfy the statement that &amp;lt;math&amp;gt;S_1, \ldots, S_{k-p}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+p}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, then the needle &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''does not match'' &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, ''i.e.'', we can ''rule out'' the position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;. On the other hand, if &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; ''does'' satisfy this statement, then &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''might'' match &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, and, in fact, all the characters up to but not including &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt; have already been verified to match the corresponding characters in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, so we can proceed by comparing &amp;lt;math&amp;gt;S_{k-p+1}&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;, and, as promised, ''never need to look back''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': Let &amp;lt;math&amp;gt;0 \leq q &amp;lt; k&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then by definition we have &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;. But since &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, S_{j+k-1}&amp;lt;/math&amp;gt;, it is also true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. If, on the other hand, it is not true that &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;k-q&amp;lt;/math&amp;gt; is a possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; if and only if &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;. Since the maximum possible value of &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\pi_k&amp;lt;/math&amp;gt;, the minimum possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is given by &amp;lt;math&amp;gt;k-\pi_k&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': Let &amp;lt;math&amp;gt;0 \leq q &amp;lt; k&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then by definition we have &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;. But since &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, S_{j+k-1}&amp;lt;/math&amp;gt;, it is also true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. If, on the other hand, it is not true that &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;k-q&amp;lt;/math&amp;gt; is a possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; if and only if &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;. Since the maximum possible value of &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\pi_k&amp;lt;/math&amp;gt;, the minimum possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is given by &amp;lt;math&amp;gt;k-\pi_k&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Thus, here is the matching algorithm in pseudocode:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;j &amp;amp;larr; 1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;k &amp;amp;larr; 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;while j+m-1 &amp;amp;le; n&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; while k &amp;amp;le; m and S[k+1] = T[j+k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; k+1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; if k = m&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; print &amp;quot;Match at position &amp;quot; j&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; if k = 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; j &amp;amp;larr; j+1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; else&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; j &amp;amp;larr; j+k-&amp;amp;pi;[k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Thus, we scan the text one character at a time; the current character being examined is located at position &amp;lt;math&amp;gt;j+k&amp;lt;/math&amp;gt;. When there is a mismatch, we use the &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; table to look up the next possible position at which the match might occur, and try to proceed.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;The fact that the algorithm scans one character at a time without looking back is more obvious when the code is cast into this equivalent form:&amp;lt;ref name=&amp;quot;CLRS&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;k &amp;amp;larr; 0&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;for i &amp;amp;isin; [1..n]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; while k &amp;amp;gt; 0 and S[k+1] &amp;amp;ne; T[i]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; if S[k+1] = T[i]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; k+1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; if k = m&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; print &amp;quot;Match at position &amp;quot; i-m+1&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; k &amp;amp;larr; &amp;amp;pi;[k]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Here, &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; is identified with &amp;lt;code&amp;gt;j+k&amp;lt;/code&amp;gt; as above. Each iteration of the inner loop in one of these two segments corresponds to an iteration of the outer loop in the other. In this second form, we can also prove that the algorithm takes &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; time; each time the inner while loop is executed, the value of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; decreases, but it cannot decrease more than &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; times because it starts as zero, is never negative, and is increased at most once per iteration of the outer loop (''i.e.'', at most &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; times in total), hence the inner loop is only executed up to &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; times. All other operations in the outer loop take constant time.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1305&amp;oldid=prev</id>
		<title>Brian at 01:58, 6 April 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1305&amp;oldid=prev"/>
				<updated>2011-04-06T01:58:39Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 01:58, 6 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L27&quot; &gt;Line 27:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 27:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it is trivially true that &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. That is, &amp;lt;math&amp;gt;\pi_k = j&amp;lt;/math&amp;gt;. By definition, &amp;lt;math&amp;gt;S^j \sqsupset S^k&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt; by assumption. Since &amp;lt;math&amp;gt;\sqsupset&amp;lt;/math&amp;gt; is [[Partial order|transitive]], &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it is trivially true that &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. That is, &amp;lt;math&amp;gt;\pi_k = j&amp;lt;/math&amp;gt;. By definition, &amp;lt;math&amp;gt;S^j \sqsupset S^k&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt; by assumption. Since &amp;lt;math&amp;gt;\sqsupset&amp;lt;/math&amp;gt; is [[Partial order|transitive]], &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;j \in \pi^*_i&amp;lt;/math&amp;gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt;. Since the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is a suffix of the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, it follows that the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt;. But the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; also matches &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt; matches the suffix of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;j \in \pi^*_i&amp;lt;/math&amp;gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt;. Since the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is a suffix of the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, it follows that the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt;. But the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; also matches &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt; matches the suffix of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find some &amp;lt;math&amp;gt;j &amp;gt; 0&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;S_j \sqsupset S_i&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find such &amp;lt;math&amp;gt;j &amp;gt; 0&amp;lt;/math&amp;gt;, then by removing the last character from this suffix, we obtain a suffix of &amp;lt;math&amp;gt;S^{i-1}&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, ''i.e.'', &amp;lt;math&amp;gt;S^{j-1} \sqsupset S^{i-1}&amp;lt;/math&amp;gt;. Therefore, we first enumerate ''all'' nonempty proper suffixes of &amp;lt;math&amp;gt;S^{i-1}&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. If we find such a suffix of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which also satisfies &amp;lt;math&amp;gt;S_k = S_i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;S^{k+1} \sqsupset S^i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;k+1&amp;lt;/math&amp;gt; is a possible value of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;_i&lt;/del&gt;&amp;lt;/math&amp;gt; is decreasing and since it contains all possible valid &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;'s. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find some &amp;lt;math&amp;gt;j &amp;gt; 0&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;S_j \sqsupset S_i&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find such &amp;lt;math&amp;gt;j &amp;gt; 0&amp;lt;/math&amp;gt;, then by removing the last character from this suffix, we obtain a suffix of &amp;lt;math&amp;gt;S^{i-1}&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, ''i.e.'', &amp;lt;math&amp;gt;S^{j-1} \sqsupset S^{i-1}&amp;lt;/math&amp;gt;. Therefore, we first enumerate ''all'' nonempty proper suffixes of &amp;lt;math&amp;gt;S^{i-1}&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. If we find such a suffix of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which also satisfies &amp;lt;math&amp;gt;S_k = S_i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;S^{k+1} \sqsupset S^i&amp;lt;/math&amp;gt;, and &amp;lt;math&amp;gt;k+1&amp;lt;/math&amp;gt; is a possible value of &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;_{i-1}&lt;/ins&gt;&amp;lt;/math&amp;gt; is decreasing and since it contains all possible valid &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;'s. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L47&quot; &gt;Line 47:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 47:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? Here is the result at the core of the KMP algorithm:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? Here is the result at the core of the KMP algorithm:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Theorem'': If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;p = k - \pi_k&amp;lt;/math&amp;gt; is the least &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;S_1, \ldots, S_{k-p}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+p}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, respectively. (If &amp;lt;math&amp;gt;k = 0&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;p = 1&amp;lt;/math&amp;gt; vacuously.)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Theorem'': If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;p = k - \pi_k&amp;lt;/math&amp;gt; is the least &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;S_1, \ldots, S_{k-p}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+p}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, respectively. (If &amp;lt;math&amp;gt;k = 0&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;p = 1&amp;lt;/math&amp;gt; vacuously.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Think carefully about what this means. If &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; does not satisfy the statement that &amp;lt;math&amp;gt;S_1, \ldots, S_{k-p}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+p}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, then the needle &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''does not match'' &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, ''i.e.'', we can ''rule out'' the position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;. On the other hand, if &amp;lt;math&amp;gt;p &amp;gt; 0&amp;lt;/math&amp;gt; ''does'' satisfy this statement, then &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; ''might'' match &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;j+p&amp;lt;/math&amp;gt;, and, in fact, all the characters up to but not including &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt; have already been verified to match the corresponding characters in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, so we can proceed by comparing &amp;lt;math&amp;gt;S_{k-p+1}&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;, and, as promised, ''never need to look back''.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;:''Proof'': Let &amp;lt;math&amp;gt;0 \leq q &amp;lt; k&amp;lt;/math&amp;gt;. If &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then by definition we have &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;. But since &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, S_{j+k-1}&amp;lt;/math&amp;gt;, it is also true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. If, on the other hand, it is not true that &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;, then it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = S_{k-q+1}, \ldots, S_k&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_{k-q+1}, \ldots, S_k = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, so it is not true that &amp;lt;math&amp;gt;S_1, \ldots, S_q = T_{j+k-q}, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;. Therefore &amp;lt;math&amp;gt;k-q&amp;lt;/math&amp;gt; is a possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; if and only if &amp;lt;math&amp;gt;S^q \sqsupset S^k&amp;lt;/math&amp;gt;. Since the maximum possible value of &amp;lt;math&amp;gt;q&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;\pi_k&amp;lt;/math&amp;gt;, the minimum possible value of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is given by &amp;lt;math&amp;gt;k-\pi_k&amp;lt;/math&amp;gt;. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1303&amp;oldid=prev</id>
		<title>Brian at 01:17, 6 April 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1303&amp;oldid=prev"/>
				<updated>2011-04-06T01:17:59Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 01:17, 6 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L45&quot; &gt;Line 45:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 45:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Suppose that we have already computed the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. Here's where we apply this hard-won information. Suppose that so far we are testing position &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; for a match, and that the first &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; characters of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; have been successfully matched, that is, &amp;lt;math&amp;gt;S_1 = T_j, S_2 = T_{j+1}, S_3 = T_{j+2}, \ldots S_k = T_{j+k-1}&amp;lt;/math&amp;gt;. There are two possibilities: either we just continue going along &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; and comparing pairs of characters, or we decide we want to try out a new position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. This occurs because either &amp;lt;math&amp;gt;k = m&amp;lt;/math&amp;gt; (''i.e'', we've successfully located &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and now we want to check out other positions) or because &amp;lt;math&amp;gt;S_{k+1} \neq T_{j+k}&amp;lt;/math&amp;gt; (so we can rule out the current position).&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Suppose that we have already computed the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. Here's where we apply this hard-won information. Suppose that so far we are testing position &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; for a match, and that the first &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; characters of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; have been successfully matched, that is, &amp;lt;math&amp;gt;S_1 = T_j, S_2 = T_{j+1}, S_3 = T_{j+2}, \ldots S_k = T_{j+k-1}&amp;lt;/math&amp;gt;. There are two possibilities: either we just continue going along &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; and comparing pairs of characters, or we decide we want to try out a new position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. This occurs because either &amp;lt;math&amp;gt;k = m&amp;lt;/math&amp;gt; (''i.e'', we've successfully located &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and now we want to check out other positions) or because &amp;lt;math&amp;gt;S_{k+1} \neq T_{j+k}&amp;lt;/math&amp;gt; (so we can rule out the current position).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;The key &lt;/del&gt;is &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;to realize&lt;/del&gt;:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Here &lt;/ins&gt;is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;the result at the core of the KMP algorithm&lt;/ins&gt;:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the next possible position in &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;T&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;j + &lt;/del&gt;k - \pi_k&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, whereas if &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;k = &lt;/del&gt;0&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;then it is trivially &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j + 1&amp;lt;/math&amp;gt;.&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;:''Theorem''&lt;/ins&gt;: If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;p = &lt;/ins&gt;k - \pi_k&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is the least &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;p &amp;gt; &lt;/ins&gt;0&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;such that &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S_1&lt;/ins&gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\ldots, S_&lt;/ins&gt;{k-&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;p&lt;/ins&gt;}&amp;lt;/math&amp;gt; match &amp;lt;math&amp;gt;T_{j+&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;p&lt;/ins&gt;}&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, &lt;/ins&gt;\&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;ldots&lt;/ins&gt;, T_{j+k&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;-1&lt;/ins&gt;}&amp;lt;/math&amp;gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;respectively. (If &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;k = 0&lt;/ins&gt;&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;p = &lt;/ins&gt;1&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;vacuously&lt;/ins&gt;.&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;That is&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;if currently the character &amp;lt;math&amp;gt;T_&lt;/del&gt;{&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j+&lt;/del&gt;k-&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;1&lt;/del&gt;}&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is known to &lt;/del&gt;match &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;math&amp;gt;S_k&amp;lt;/math&amp;gt; (and all the preceding characters match), then we also know that &lt;/del&gt;&amp;lt;math&amp;gt;T_{j+&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;k-1&lt;/del&gt;}&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;S_{&lt;/del&gt;\&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;pi_k}&amp;lt;/math&amp;gt; and all the preceding characters match&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;and should continue our search by comparing &amp;lt;math&amp;gt;S_{\pi_k+1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;&lt;/del&gt;T_{j+k}&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;. On the other hand&lt;/del&gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;if even &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_1&amp;lt;/math&amp;gt; does not match the corresponding &amp;lt;math&amp;gt;T_j&lt;/del&gt;&amp;lt;/math&amp;gt;, then &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;we have absolutely no information about whether &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S&amp;lt;/math&amp;gt; may occur starting at &amp;lt;math&amp;gt;T_{j+&lt;/del&gt;1&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;}&lt;/del&gt;&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, and we must simply proceed as in the naive algorithm&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1286&amp;oldid=prev</id>
		<title>Brian: /* Matching */ - sp.</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1286&amp;oldid=prev"/>
				<updated>2011-04-04T05:43:33Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Matching: &lt;/span&gt; - sp.&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 05:43, 4 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L47&quot; &gt;Line 47:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 47:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? The key is to realize:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? The key is to realize:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then the next possible position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;j + k - \pi_k&amp;lt;/math&amp;gt;, whereas if &amp;lt;math&amp;gt;k = 0&amp;lt;/math&amp;gt; then it is trivially &amp;lt;math&amp;gt;j + 1&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then the next possible position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;j + k - \pi_k&amp;lt;/math&amp;gt;, whereas if &amp;lt;math&amp;gt;k = 0&amp;lt;/math&amp;gt; then it is trivially &amp;lt;math&amp;gt;j + 1&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;That is, if currently the character &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; is known to match &amp;lt;math&amp;gt;S_k&amp;lt;/math&amp;gt; (and all the preceding characters match), then we also know that &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;S_{\pi_k}&amp;lt;/math&amp;gt; and all the preceding characters match, and should continue &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;or &lt;/del&gt;search by comparing &amp;lt;math&amp;gt;S_{\pi_k+1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;. On the other hand, if even &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; does not match the corresponding &amp;lt;math&amp;gt;T_j&amp;lt;/math&amp;gt;, then we have absolutely no information about whether &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; may occur starting at &amp;lt;math&amp;gt;T_{j+1}&amp;lt;/math&amp;gt;, and we must simply proceed as in the naive algorithm.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;That is, if currently the character &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; is known to match &amp;lt;math&amp;gt;S_k&amp;lt;/math&amp;gt; (and all the preceding characters match), then we also know that &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;S_{\pi_k}&amp;lt;/math&amp;gt; and all the preceding characters match, and should continue &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;our &lt;/ins&gt;search by comparing &amp;lt;math&amp;gt;S_{\pi_k+1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;. On the other hand, if even &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; does not match the corresponding &amp;lt;math&amp;gt;T_j&amp;lt;/math&amp;gt;, then we have absolutely no information about whether &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; may occur starting at &amp;lt;math&amp;gt;T_{j+1}&amp;lt;/math&amp;gt;, and we must simply proceed as in the naive algorithm.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1285&amp;oldid=prev</id>
		<title>Brian at 05:43, 4 April 2011</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1285&amp;oldid=prev"/>
				<updated>2011-04-04T05:43:05Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 05:43, 4 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L41&quot; &gt;Line 41:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 41:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This algorithm takes &amp;lt;math&amp;gt;O(m)&amp;lt;/math&amp;gt; time to execute. To see this, notice that the value of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; is never negative; therefore it cannot decrease more than it increases. It only increases in the line &amp;lt;code&amp;gt;k &amp;amp;larr; k+1&amp;lt;/code&amp;gt;, which can only be executed up to &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; times. Therefore &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; can be decreased at most &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; times. But &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; is decreased in each iteration of the while loop, so the while loop can only run a linear number of times overall. All the other instructions in the for loop take constant time per iteration, so linear time overall.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This algorithm takes &amp;lt;math&amp;gt;O(m)&amp;lt;/math&amp;gt; time to execute. To see this, notice that the value of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; is never negative; therefore it cannot decrease more than it increases. It only increases in the line &amp;lt;code&amp;gt;k &amp;amp;larr; k+1&amp;lt;/code&amp;gt;, which can only be executed up to &amp;lt;math&amp;gt;m-1&amp;lt;/math&amp;gt; times. Therefore &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; can be decreased at most &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; times. But &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; is decreased in each iteration of the while loop, so the while loop can only run a linear number of times overall. All the other instructions in the for loop take constant time per iteration, so linear time overall.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;==Matching==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Suppose that we have already computed the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. Here's where we apply this hard-won information. Suppose that so far we are testing position &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; for a match, and that the first &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; characters of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; have been successfully matched, that is, &amp;lt;math&amp;gt;S_1 = T_j, S_2 = T_{j+1}, S_3 = T_{j+2}, \ldots S_k = T_{j+k-1}&amp;lt;/math&amp;gt;. There are two possibilities: either we just continue going along &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; and comparing pairs of characters, or we decide we want to try out a new position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;. This occurs because either &amp;lt;math&amp;gt;k = m&amp;lt;/math&amp;gt; (''i.e'', we've successfully located &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; at position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt;, and now we want to check out other positions) or because &amp;lt;math&amp;gt;S_{k+1} \neq T_{j+k}&amp;lt;/math&amp;gt; (so we can rule out the current position).&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Given that &amp;lt;math&amp;gt;S_1, \ldots, S_k = T_j, \ldots, T_{j+k-1}&amp;lt;/math&amp;gt;, what positions in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; can we rule out? The key is to realize:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;:If &amp;lt;math&amp;gt;k &amp;gt; 0&amp;lt;/math&amp;gt; then the next possible position in &amp;lt;math&amp;gt;T&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;j + k - \pi_k&amp;lt;/math&amp;gt;, whereas if &amp;lt;math&amp;gt;k = 0&amp;lt;/math&amp;gt; then it is trivially &amp;lt;math&amp;gt;j + 1&amp;lt;/math&amp;gt;.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;That is, if currently the character &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; is known to match &amp;lt;math&amp;gt;S_k&amp;lt;/math&amp;gt; (and all the preceding characters match), then we also know that &amp;lt;math&amp;gt;T_{j+k-1}&amp;lt;/math&amp;gt; matches &amp;lt;math&amp;gt;S_{\pi_k}&amp;lt;/math&amp;gt; and all the preceding characters match, and should continue or search by comparing &amp;lt;math&amp;gt;S_{\pi_k+1}&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;T_{j+k}&amp;lt;/math&amp;gt;. On the other hand, if even &amp;lt;math&amp;gt;S_1&amp;lt;/math&amp;gt; does not match the corresponding &amp;lt;math&amp;gt;T_j&amp;lt;/math&amp;gt;, then we have absolutely no information about whether &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; may occur starting at &amp;lt;math&amp;gt;T_{j+1}&amp;lt;/math&amp;gt;, and we must simply proceed as in the naive algorithm.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==References==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;references/&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1284&amp;oldid=prev</id>
		<title>Brian: notation</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1284&amp;oldid=prev"/>
				<updated>2011-04-04T03:35:26Z</updated>
		
		<summary type="html">&lt;p&gt;notation&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 03:35, 4 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L27&quot; &gt;Line 27:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 27:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it is trivially true that &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. That is, &amp;lt;math&amp;gt;\pi_k = j&amp;lt;/math&amp;gt;. By definition, &amp;lt;math&amp;gt;S^j \sqsupset S^k&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt; by assumption. Since &amp;lt;math&amp;gt;\sqsupset&amp;lt;/math&amp;gt; is [[Partial order|transitive]], &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it is trivially true that &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. That is, &amp;lt;math&amp;gt;\pi_k = j&amp;lt;/math&amp;gt;. By definition, &amp;lt;math&amp;gt;S^j \sqsupset S^k&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt; by assumption. Since &amp;lt;math&amp;gt;\sqsupset&amp;lt;/math&amp;gt; is [[Partial order|transitive]], &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;j \in \pi^*_i&amp;lt;/math&amp;gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt;. Since the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is a suffix of the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, it follows that the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt;. But the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; also matches &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt; matches the suffix of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &amp;lt;math&amp;gt;S^j \sqsupset S^i&amp;lt;/math&amp;gt;, then &amp;lt;math&amp;gt;j \in \pi^*_i&amp;lt;/math&amp;gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &amp;lt;math&amp;gt;S^k \sqsupset S^i&amp;lt;/math&amp;gt;. Since the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is a suffix of the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, it follows that the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt;. But the suffix of &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; also matches &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;S^j&amp;lt;/math&amp;gt; matches the suffix of &amp;lt;math&amp;gt;S^k&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;a nonempty proper suffix of the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;i&lt;/del&gt;&amp;lt;/math&amp;gt; that &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is also a prefix of &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S&lt;/del&gt;&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;a nonempty proper suffix of &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_i&amp;lt;/math&lt;/del&gt;&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, and that this suffix has length &amp;lt;math&amp;gt;k&lt;/del&gt;&amp;lt;/math&amp;gt;, then&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, &lt;/del&gt;by removing the last character from this suffix, we obtain a &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;nonempty proper &lt;/del&gt;suffix of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;if &lt;/del&gt;we enumerate ''all'' nonempty proper suffixes of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, the ''longest'' &lt;/del&gt;of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;these ''that can be extended'' (because the character after this prefix equals the character &lt;/del&gt;&amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;so that it can be added to the end of both the prefix and the suffix to give a suffix of the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;that is also a prefix of &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S&lt;/del&gt;&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;) gives the &lt;/del&gt;value of &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;\pi_i&lt;/del&gt;&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is decreasing. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;some &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j &amp;gt; 0&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;such &lt;/ins&gt;that &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S_j \sqsupset S_i&lt;/ins&gt;&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;such &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j &lt;/ins&gt;&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;0&lt;/ins&gt;&amp;lt;/math&amp;gt;, then by removing the last character from this suffix, we obtain a suffix of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^{&lt;/ins&gt;i-1&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;}&lt;/ins&gt;&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt;, ''i.e.'', &amp;lt;math&amp;gt;S^{j-1} \sqsupset S^{i-1}&lt;/ins&gt;&amp;lt;/math&amp;gt;. Therefore, we &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;first &lt;/ins&gt;enumerate ''all'' nonempty proper suffixes of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^{&lt;/ins&gt;i-1&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;}&lt;/ins&gt;&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. If we find such a suffix &lt;/ins&gt;of &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;length &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;k&amp;lt;/math&amp;gt; which also satisfies &amp;lt;math&amp;gt;S_k = &lt;/ins&gt;S_i&amp;lt;/math&amp;gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^{k+1} \sqsupset S^&lt;/ins&gt;i&amp;lt;/math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, and &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;k+1&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is a possible &lt;/ins&gt;value of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/ins&gt;&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is decreasing &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;and since it contains all possible valid &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;'s&lt;/ins&gt;. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the pseudocode:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1283&amp;oldid=prev</id>
		<title>Brian: notation</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm&amp;diff=1283&amp;oldid=prev"/>
				<updated>2011-04-04T02:13:30Z</updated>
		
		<summary type="html">&lt;p&gt;notation&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 02:13, 4 April 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L13&quot; &gt;Line 13:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 13:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Concept==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Concept==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;''Let the prefix of length &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; of string &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; be denoted &amp;lt;math&amp;gt;S^i&amp;lt;/math&amp;gt;.''&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The examples above show that the KMP algorithm relies on noticing that certain substrings of the needle match or do not match other substrings of the needle, but it is probably not clear what the unifying organizational principle for all this match information is. Here it is:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The examples above show that the KMP algorithm relies on noticing that certain substrings of the needle match or do not match other substrings of the needle, but it is probably not clear what the unifying organizational principle for all this match information is. Here it is:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''At each position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, find the longest proper suffix &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;of the prefix &lt;/del&gt;of &amp;lt;math&amp;gt;S&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;&lt;/del&gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.''&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''At each position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, find the longest proper suffix of &amp;lt;math&amp;gt;S&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;^&lt;/ins&gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.''&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We shall denote the length of this substring by &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt;, following &amp;lt;ref name=&amp;quot;CLRS&amp;quot;&amp;gt;Thomas H. Cormen; Charles E. Leiserson, Ronald L. Rivest, Clifford Stein (2001). &amp;quot;Section 32.4: The Knuth-Morris-Pratt algorithm&amp;quot;. ''Introduction to Algorithms'' (Second ed.). MIT Press and McGraw-Hill. pp. 923–931. ISBN 978-0-262-03293-3.&amp;lt;/ref&amp;gt;. We can also state the definition of &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt; equivalently as ''the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;length of the longest substring with length less than &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;i&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ending at &lt;/del&gt;&amp;lt;math&amp;gt;S_i&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; which is also a prefix of &amp;lt;math&amp;gt;S&lt;/del&gt;&amp;lt;/math&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We shall denote the length of this substring by &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt;, following &amp;lt;ref name=&amp;quot;CLRS&amp;quot;&amp;gt;Thomas H. Cormen; Charles E. Leiserson, Ronald L. Rivest, Clifford Stein (2001). &amp;quot;Section 32.4: The Knuth-Morris-Pratt algorithm&amp;quot;. ''Introduction to Algorithms'' (Second ed.). MIT Press and McGraw-Hill. pp. 923–931. ISBN 978-0-262-03293-3.&amp;lt;/ref&amp;gt;. We can also state the definition of &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt; equivalently as ''the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;maximum &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;such that &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S_j \sqsupset &lt;/ins&gt;S_i&amp;lt;/math&amp;gt;''.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;, called the ''prefix function'', occupies linear space, and, as we shall see, can be computed in linear time. It contains ''all'' the information we need in order to execute the &amp;quot;smart&amp;quot; searching techniques described in the examples. In particular, in examples 1 and 2, we used the fact that &amp;lt;math&amp;gt;\pi_3 = 2&amp;lt;/math&amp;gt;, that is, the prefix '''aa''' matches the suffix '''aa'''. In example 3, we used the facts that &amp;lt;math&amp;gt;\pi_5 = 2&amp;lt;/math&amp;gt;. This tells us that the prefix '''ta''' matches the substring '''ta''' ending at the fifth position. In general, the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; tells us, after either a successful match or a mismatch, what the next position is that we should check in the haystack. Comparison proceeds from where it was left off, never revisiting a character of the haystack after we have examined the next one.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;, called the ''prefix function'', occupies linear space, and, as we shall see, can be computed in linear time. It contains ''all'' the information we need in order to execute the &amp;quot;smart&amp;quot; searching techniques described in the examples. In particular, in examples 1 and 2, we used the fact that &amp;lt;math&amp;gt;\pi_3 = 2&amp;lt;/math&amp;gt;, that is, the prefix '''aa''' matches the suffix '''aa'''. In example 3, we used the facts that &amp;lt;math&amp;gt;\pi_5 = 2&amp;lt;/math&amp;gt;. This tells us that the prefix '''ta''' matches the substring '''ta''' ending at the fifth position. In general, the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt; tells us, after either a successful match or a mismatch, what the next position is that we should check in the haystack. Comparison proceeds from where it was left off, never revisiting a character of the haystack after we have examined the next one.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L21&quot; &gt;Line 21:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Computation of the prefix function==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Computation of the prefix function==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;To compute the prefix function, we shall first make the following observation:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;To compute the prefix function, we shall first make the following observation:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Prefix function iteration lemma''&amp;lt;ref name=&amp;quot;CLRS&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;: The sequence &amp;lt;math&amp;gt;\pi^*_i = i, \pi_i, \pi_{\pi_i}, \pi_{\pi_{\pi_i}}, \ldots, 0&amp;lt;/math&amp;gt; contains exactly those values &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; such that &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of &lt;/del&gt;&amp;lt;math&amp;gt;S&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;&lt;/del&gt;j&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; matches the substring of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; ending at &amp;lt;math&amp;gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Prefix function iteration lemma''&amp;lt;ref name=&amp;quot;CLRS&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;: The sequence &amp;lt;math&amp;gt;\pi^*_i = i, \pi_i, \pi_{\pi_i}, \pi_{\pi_{\pi_i}}, \ldots, 0&amp;lt;/math&amp;gt; contains exactly those values &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;S&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;^&lt;/ins&gt;j &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset S^i&lt;/ins&gt;&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;That is, we can enumerate all &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;substrings ending at &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt; that are prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; by starting with &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, looking it up in the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;, looking up the result, looking up the result, and so on, giving a strictly decreasing sequence, terminating with zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;That is, we can enumerate all &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;suffixes of &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^i&lt;/ins&gt;&amp;lt;/math&amp;gt; that are &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;also &lt;/ins&gt;prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; by starting with &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, looking it up in the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;, looking up the result, looking up the result, and so on, giving a strictly decreasing sequence, terminating with zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_1, S_2, &lt;/del&gt;\&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ldots, S_j&amp;lt;/math&amp;gt; matches the substring &amp;lt;math&amp;gt;S_{&lt;/del&gt;i&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;-j+1}, S_{i-j+2}, \ldots, S_i&lt;/del&gt;&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it trivially &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;belongs (''i.e'', the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;matches itself)&lt;/del&gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;This means that the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is a suffix of the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;. But &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;k&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; matches the substring of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; ending at &amp;lt;math&amp;gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt; by assumption. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;So the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/del&gt;&amp;lt;/math&amp;gt; is &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;a suffix of the substring of length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; ending at &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt;. Therefore the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;matches the substring of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; ending at &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt; (since &amp;lt;math&amp;gt;j &amp;lt; k&amp;lt;/math&amp;gt;)&lt;/del&gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:''Proof'': We first show by induction that if &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; appears in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; then &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^j &lt;/ins&gt;\&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;sqsupset S^&lt;/ins&gt;i&amp;lt;/math&amp;gt;, ''i.e'', &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; indeed belongs in the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is the first entry in &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt;. Then &amp;lt;math&amp;gt;j = i&amp;lt;/math&amp;gt; and it &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;is &lt;/ins&gt;trivially &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;true that &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^j \sqsupset S^&lt;/ins&gt;i&amp;lt;/math&amp;gt;. Now suppose &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is not the first entry, but is preceded by the entry &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; which is valid. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;That is, &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\pi_k = &lt;/ins&gt;j&amp;lt;/math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. By definition, &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^j \sqsupset S^&lt;/ins&gt;k&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;k &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset S^i&lt;/ins&gt;&amp;lt;/math&amp;gt; by assumption. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Since &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset&lt;/ins&gt;&amp;lt;/math&amp;gt; is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;[[Partial order|transitive]], &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;j &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset S^i&lt;/ins&gt;&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;j&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of the prefix of length &amp;lt;math&amp;gt;&lt;/del&gt;i&amp;lt;/math&amp;gt;, &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;''i.e.'', if &lt;/del&gt;&amp;lt;math&amp;gt;j&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; &amp;quot;belongs&amp;quot; &lt;/del&gt;in &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the sequence &amp;lt;math&amp;gt;&lt;/del&gt;\pi^*_i&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;, then it appears in this sequence&lt;/del&gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the substring ending at &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt; with length &lt;/del&gt;&amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is also a prefix &lt;/del&gt;of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;. Since the substring &lt;/del&gt;of length &amp;lt;math&amp;gt;j&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/math&amp;gt; ending at &amp;lt;math&amp;gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt; is a suffix of the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;substring &lt;/del&gt;of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;k&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ending at &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt;, it follows that the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;substring &lt;/del&gt;of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ending at &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;. But the &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;substring &lt;/del&gt;of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;ending at &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;S_i&lt;/del&gt;&amp;lt;/math&amp;gt; also matches &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt;, so &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;the prefix of length &lt;/del&gt;&amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; matches the suffix of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;length &lt;/del&gt;&amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/del&gt;&amp;lt;/math&amp;gt; &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;of the prefix &lt;/del&gt;of length &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;k&lt;/del&gt;&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;:We now show by contradiction that if &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;j &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset S^&lt;/ins&gt;i&amp;lt;/math&amp;gt;, &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;then &lt;/ins&gt;&amp;lt;math&amp;gt;j &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\&lt;/ins&gt;in \pi^*_i&amp;lt;/math&amp;gt;. Assume &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; does not appear in the sequence. Clearly &amp;lt;math&amp;gt;0 &amp;lt; j &amp;lt; i&amp;lt;/math&amp;gt; since 0 and &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; both appear. Since &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is strictly decreasing, we can find exactly one &amp;lt;math&amp;gt;k \in \pi^*_i&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;k &amp;gt; j&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\pi_k &amp;lt; j&amp;lt;/math&amp;gt;; that is, we can find exactly one &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; after which &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; &amp;quot;should&amp;quot; appear (to keep the sequence decreasing). We know from the first part of the proof that &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;k &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;\sqsupset S^i&lt;/ins&gt;&amp;lt;/math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. Since the suffix &lt;/ins&gt;of &amp;lt;math&amp;gt;S&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;^i&lt;/ins&gt;&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; is a suffix of the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;suffix &lt;/ins&gt;of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^i&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;of length &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;k&lt;/ins&gt;&amp;lt;/math&amp;gt;, it follows that the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;suffix &lt;/ins&gt;of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^i&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;of length &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/ins&gt;&amp;lt;/math&amp;gt; matches the suffix of length &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;k&amp;lt;/math&amp;gt;. But the &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;suffix &lt;/ins&gt;of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^i&lt;/ins&gt;&amp;lt;/math&amp;gt; &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;of length &lt;/ins&gt;&amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/ins&gt;&amp;lt;/math&amp;gt; also matches &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;j&amp;lt;/math&amp;gt;, so &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^&lt;/ins&gt;j&amp;lt;/math&amp;gt; matches the suffix of &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;S^k&lt;/ins&gt;&amp;lt;/math&amp;gt; of length &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;j&lt;/ins&gt;&amp;lt;/math&amp;gt;. We therefore conclude that &amp;lt;math&amp;gt;\pi_k \geq j&amp;lt;/math&amp;gt;. But &amp;lt;math&amp;gt;j &amp;gt; \pi_k&amp;lt;/math&amp;gt;, a contradiction. &amp;lt;math&amp;gt;_\blacksquare&amp;lt;/math&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find a nonempty proper suffix of the prefix of length &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find a nonempty proper suffix of &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, and that this suffix has length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, then, by removing the last character from this suffix, we obtain a nonempty proper suffix of the prefix of length &amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, if we enumerate ''all'' nonempty proper suffixes of the prefix of length &amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, the ''longest'' of these ''that can be extended'' (because the character after this prefix equals the character &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt;, so that it can be added to the end of both the prefix and the suffix to give a suffix of the prefix of length &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;) gives the value of &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is decreasing. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;With this in mind, we can design an algorithm to compute the table &amp;lt;math&amp;gt;\pi&amp;lt;/math&amp;gt;. For each &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt;, we will first try to find a nonempty proper suffix of the prefix of length &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. If we fail to do so, we will conclude that &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt; (clearly this is the case when &amp;lt;math&amp;gt;i = 1&amp;lt;/math&amp;gt;.) Observe that if we do find a nonempty proper suffix of &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, and that this suffix has length &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, then, by removing the last character from this suffix, we obtain a nonempty proper suffix of the prefix of length &amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Therefore, if we enumerate ''all'' nonempty proper suffixes of the prefix of length &amp;lt;math&amp;gt;i-1&amp;lt;/math&amp;gt; that are also prefixes of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, the ''longest'' of these ''that can be extended'' (because the character after this prefix equals the character &amp;lt;math&amp;gt;S_i&amp;lt;/math&amp;gt;, so that it can be added to the end of both the prefix and the suffix to give a suffix of the prefix of length &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; that is also a prefix of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;) gives the value of &amp;lt;math&amp;gt;\pi_i&amp;lt;/math&amp;gt;. So we will let &amp;lt;math&amp;gt;k = \pi_{i-1}&amp;lt;/math&amp;gt; and keep iterating through the sequence &amp;lt;math&amp;gt;\pi_k, \pi_{\pi_k}, \ldots&amp;lt;/math&amp;gt;. We stop if we reach element &amp;lt;math&amp;gt;j&amp;lt;/math&amp;gt; in this sequence such that &amp;lt;math&amp;gt;S_{j+1} = S_i&amp;lt;/math&amp;gt;, and declare &amp;lt;math&amp;gt;\pi_i = j+1&amp;lt;/math&amp;gt;; this always gives an optimal solution since the sequence &amp;lt;math&amp;gt;\pi^*_i&amp;lt;/math&amp;gt; is decreasing. If we exhaust the sequence, then &amp;lt;math&amp;gt;\pi_i = 0&amp;lt;/math&amp;gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>