KMP字符串模式匹配详解

来源:互联网 发布:织梦 资讯新闻类 模板 编辑:程序博客网 时间:2024/06/06 00:37

最近看严蔚敏的数据结构,第四章的KMP算法很有意思,刚开始没看太懂,后来找到这篇文章,思路清晰多了。

                                          KMP字符串模式匹配详解

KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法。简单匹配算法的时间复杂度为O(m*n),KMP匹配算法可以证明它的时间复杂度为O(m+n)

.简单匹配算法

先来看一个简单匹配算法的函数:
int Index_BF ( char S [ ], char T [ ], int pos )  {  /* 若串 S 中从第pos(S 的下标0≤pos<StrLength(S))个字符 起存在和串 T 相同的子串,则称匹配成功,返回第一个 这样的子串在串 S 中的下标,否则返回 -1    */  int i = pos, j = 0;  while ( S[i+j] != '/0'&& T[j] != '/0')  if ( S[i+j] == T[j] )  j ++; // 继续比较后一字符  else  {  i ++; j = 0; // 重新开始新的一轮匹配  }  if ( T[j] == '/0')  return i; // 匹配成功   返回下标  else  return -1; // 串S中(第pos个字符起)不存在和串T相同的子串  } // Index_BF
<div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">此算法的思想是直截了当的:将主串</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中某个位置</span><span style="font-size: 12pt; line-height: 20px;">i</span><span style="font-size: 12pt; line-height: 20px;">起始的子串和模式串</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">相比较。即从</span><span style="font-size: 12pt; line-height: 20px;"> j=0 </span><span style="font-size: 12pt; line-height: 20px;">起比较</span><span style="font-size: 12pt; line-height: 20px;">S[i+j] </span><span style="font-size: 12pt; line-height: 20px;">与</span><span style="font-size: 12pt; line-height: 20px;"> T[j]</span><span style="font-size: 12pt; line-height: 20px;">,若相等,则在主串</span><span style="font-size: 12pt; line-height: 20px;"> S </span><span style="font-size: 12pt; line-height: 20px;">中存在以</span><span style="font-size: 12pt; line-height: 20px;"> i </span><span style="font-size: 12pt; line-height: 20px;">为起始位置匹配成功的可能性,继续往后比较</span><span style="font-size: 12pt; line-height: 20px;">( j</span><span style="font-size: 12pt; line-height: 20px;">逐步增</span><span style="font-size: 12pt; line-height: 20px;">1 )</span><span style="font-size: 12pt; line-height: 20px;">,直至与</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">串中最后一个字符相等为止,否则改从</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">串的下一个字符起重新开始进行下一轮的</span><span style="font-size: 12pt; line-height: 20px;">"</span><span style="font-size: 12pt; line-height: 20px;">匹配</span><span style="font-size: 12pt; line-height: 20px;">"</span><span style="font-size: 12pt; line-height: 20px;">,即将串</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">向后滑动一位,即</span><span style="font-size: 12pt; line-height: 20px;"> i </span><span style="font-size: 12pt; line-height: 20px;">增</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">,而</span><span style="font-size: 12pt; line-height: 20px;"> j </span><span style="font-size: 12pt; line-height: 20px;">退回至</span><span style="font-size: 12pt; line-height: 20px;">0</span><span style="font-size: 12pt; line-height: 20px;">,重新开始新一轮的匹配。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">例如:在串</span><span style="font-size: 12pt; line-height: 20px;">S=</span><span style="font-size: 15pt; line-height: 26px;">”abcabcabdabba”</span><span style="font-size: 15pt; line-height: 26px;">中查找</span><span style="font-size: 15pt; line-height: 26px;">T=” abcabd”</span><span style="font-size: 12pt; line-height: 20px;">(我们可以假设从下标</span><span style="font-size: 12pt; line-height: 20px;">0</span><span style="font-size: 12pt; line-height: 20px;">开始)</span><span style="font-size: 12pt; line-height: 20px;">:</span><span style="font-size: 12pt; line-height: 20px;">先是比较</span><span style="font-size: 12pt; line-height: 20px;">S[0]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">是否相等,然后比较</span><span style="font-size: 12pt; line-height: 20px;">S[1] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[1]</span><span style="font-size: 12pt; line-height: 20px;">是否相等</span><span style="font-size: 12pt; line-height: 20px;">…</span><span style="font-size: 12pt; line-height: 20px;">我们发现一直比较到</span><span style="font-size: 12pt; line-height: 20px;">S[5]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[5]</span><span style="font-size: 12pt; line-height: 20px;">才不等。如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/9e2d7a511327402bbc7959c84ebd6f98.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"> </div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">当这样一个失配发生时,</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标必须回溯到开始,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标回溯的长度与</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">相同,然后</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标增</span><span style="font-size: 12pt; line-height: 20px;">1,</span><span style="font-size: 12pt; line-height: 20px;">然后再次比较。如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">这次立刻发生了失配,</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标又回溯到开始,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标增</span><span style="font-size: 12pt; line-height: 20px;">1,</span><span style="font-size: 12pt; line-height: 20px;">然后再次比较。如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/33b15074cb1f4dcabbe3bb4c729fece8.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">这次立刻发生了失配,</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标又回溯到开始,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标增</span><span style="font-size: 12pt; line-height: 20px;">1,</span><span style="font-size: 12pt; line-height: 20px;">然后再次比较。如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/42983ca2a0314f3d82f83a771c6a4c32.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">又一次发生了失配,所以</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标又回溯到开始,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标增</span><span style="font-size: 12pt; line-height: 20px;">1,</span><span style="font-size: 12pt; line-height: 20px;">然后再次比较。这次</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">中的所有字符都和</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中相应的字符匹配了。函数返回</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">在</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中的起始下标</span><span style="font-size: 12pt; line-height: 20px;">3</span><span style="font-size: 12pt; line-height: 20px;">。如图:</span><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/7eb3b9a6e6624a0a8fe8559ba1d42129.jpg" style="border: none; max-width: 100%;" /></span></div><h2><strong><span style="font-size: 16pt; line-height: 27px;">二</span><span style="font-size: 16pt; line-height: 27px;">. KMP</span><span style="font-size: 16pt; line-height: 27px;">匹配算法</span></strong></h2><p><strong><span style="font-size: 16pt; line-height: 27px;"></span></strong><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">还是相同的例子,在</span><span style="font-size: 12pt; line-height: 20px;">S=</span><span style="font-size: 15pt; line-height: 26px;">”abcabcabdabba”</span><span style="font-size: 12pt; line-height: 20px;">中查找</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 15pt; line-height: 26px;">=”abcabd”</span><span style="font-size: 12pt; line-height: 20px;">,如果使用</span><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">匹配算法,当第一次搜索到</span><span style="font-size: 12pt; line-height: 20px;">S[5] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[5]</span><span style="font-size: 12pt; line-height: 20px;">不等后,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标不是回溯到</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标也不是回溯到开始,而是根据</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">中</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的模式函数值(</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">,为什么?后面讲),直接比较</span><span style="font-size: 12pt; line-height: 20px;">S[5] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">是否相等,因为相等,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的下标同时增加</span><span style="font-size: 12pt; line-height: 20px;">;</span><span style="font-size: 12pt; line-height: 20px;">因为又相等,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的下标又同时增加。。。最终在</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中找到了</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">。如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/fd21ec3847f94dd28f3efd5e49408167.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">匹配算法和简单匹配算法效率比较,一个极端的例子是:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">在</span><span style="font-size: 12pt; line-height: 20px;">S=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">AAAAAA…AAB</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">(100</span><span style="font-size: 12pt; line-height: 20px;">个</span><span style="font-size: 12pt; line-height: 20px;">A)</span><span style="font-size: 12pt; line-height: 20px;">中查找</span><span style="font-size: 12pt; line-height: 20px;">T=”AAAAAAAAAB”, </span><span style="font-size: 12pt; line-height: 20px;">简单匹配算法每次都是比较到</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的结尾,发现字符不同,然后</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的下标回溯到开始,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">的下标也要回溯相同长度后增</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">,继续比较。如果使用</span><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">匹配算法,就不必回溯</span><span style="font-size: 12pt; line-height: 20px;">.</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">对于一般文稿中串的匹配,简单匹配算法的时间复杂度可降为</span><span style="font-size: 12pt; line-height: 20px;">O (m+n)</span><span style="font-size: 12pt; line-height: 20px;">,因此在多数的实际应用场合下被应用。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">算法的核心思想是利用已经得到的部分匹配信息来进行后面的匹配过程。看前面的例子。为什么</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的模式函数值等于</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">(</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">),其实这个</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">表示</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的前面有</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">个字符和开始的两个字符相同,且</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">不等于开始的两个字符之后的第三个字符(</span><span style="font-size: 12pt; line-height: 20px;">T[2]=’c’</span><span style="font-size: 12pt; line-height: 20px;">)</span><span style="font-size: 12pt; line-height: 20px;">.</span><span style="font-size: 12pt; line-height: 20px;">如图:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-bottom: 12pt; text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/fe9a1d6bcb8f4e15944b3cffe6372744.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">也就是说,如果开始的两个字符之后的第三个字符也为</span><span style="font-size: 12pt; line-height: 20px;">’d’,</span><span style="font-size: 12pt; line-height: 20px;">那么,尽管</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的前面有</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">个字符和开始的两个字符相同,</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的模式函数值也不为</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">,而是为</span><span style="font-size: 12pt; line-height: 20px;">0</span><span style="font-size: 12pt; line-height: 20px;">。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">   </span><span style="font-size: 12pt; line-height: 20px;">前面我说:在</span><span style="font-size: 12pt; line-height: 20px;">S=</span><span style="font-size: 15pt; line-height: 26px;">”abcabcabdabba”</span><span style="font-size: 12pt; line-height: 20px;">中查找</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 15pt; line-height: 26px;">=”abcabd”</span><span style="font-size: 12pt; line-height: 20px;">,如果使用</span><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">匹配算法,当第一次搜索到</span><span style="font-size: 12pt; line-height: 20px;">S[5] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[5]</span><span style="font-size: 12pt; line-height: 20px;">不等后,</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">下标不是回溯到</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">下标也不是回溯到开始,而是根据</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">中</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的模式函数值,直接比较</span><span style="font-size: 12pt; line-height: 20px;">S[5] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">是否相等。。。为什么可以这样?</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 42pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">刚才我又说:“(</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">),其实这个</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">表示</span><span style="font-size: 12pt; line-height: 20px;">T[5]==’d’</span><span style="font-size: 12pt; line-height: 20px;">的前面有</span><span style="font-size: 12pt; line-height: 20px;">2</span><span style="font-size: 12pt; line-height: 20px;">个字符和开始的两个字符相同”。请看图</span><span style="font-size: 12pt; line-height: 20px;"> </span><span style="font-size: 12pt; line-height: 20px;">:因为,</span><span style="font-size: 12pt; line-height: 20px;">S[4] ==T[4]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[3] ==T[3]</span><span style="font-size: 12pt; line-height: 20px;">,根据</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">,有</span><span style="font-size: 12pt; line-height: 20px;">T[3]==T[0]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">T[4] ==T[1]</span><span style="font-size: 12pt; line-height: 20px;">,所以</span><span style="font-size: 12pt; line-height: 20px;">S[3]==T[0]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[4] ==T[1]</span><span style="font-size: 12pt; line-height: 20px;">(两对相当于间接比较过了),因此,接下来比较</span><span style="font-size: 12pt; line-height: 20px;">S[5] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">是否相等。。。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-bottom: 12pt; text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/lin_bei/f6fd94b555f340ea8e7bb46ac5b69e78.jpg" style="border: none; max-width: 100%;" /></span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">有人可能会问:</span><span style="font-size: 12pt; line-height: 20px;">S[3]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[4] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[1]</span><span style="font-size: 12pt; line-height: 20px;">是根据</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">间接比较相等,那</span><span style="font-size: 12pt; line-height: 20px;">S[1]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">之间又是怎么跳过,可以不比较呢?因为</span><span style="font-size: 12pt; line-height: 20px;">S[0]=T[0]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[1]=T[1]</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">S[2]=T[2]</span><span style="font-size: 12pt; line-height: 20px;">,而</span><span style="font-size: 12pt; line-height: 20px;">T[0] != T[1], T[1] != T[2],==> S[0] != S[1],S[1] != S[2],</span><span style="font-size: 12pt; line-height: 20px;">所以</span><span style="font-size: 12pt; line-height: 20px;">S[1] != T[0],S[2] != T[0]. </span><span style="font-size: 12pt; line-height: 20px;">还是从理论上间接比较了。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">有人疑问又来了,你分析的是不是特殊轻况啊。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">假设</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">不变,在</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中搜索</span><span style="font-size: 12pt; line-height: 20px;">T=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">abaabd</span><span style="font-size: 12pt; line-height: 20px;">”呢?答:这种情况,当比较到</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">时,发现不等,就去看</span><span style="font-size: 12pt; line-height: 20px;">next[2]</span><span style="font-size: 12pt; line-height: 20px;">的值,</span><span style="font-size: 12pt; line-height: 20px;">next[2]=-1</span><span style="font-size: 12pt; line-height: 20px;">,意思是</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">已经和</span><span style="font-size: 12pt; line-height: 20px;">T[0] </span><span style="font-size: 12pt; line-height: 20px;">间接比较过了,不相等,接下来去比较</span><span style="font-size: 12pt; line-height: 20px;">S[3]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">吧。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">假设</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">不变,在</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中搜索</span><span style="font-size: 12pt; line-height: 20px;">T=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">abbabd</span><span style="font-size: 12pt; line-height: 20px;">”呢?答:这种情况当比较到</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">时,发现不等,就去看</span><span style="font-size: 12pt; line-height: 20px;">next[2]</span><span style="font-size: 12pt; line-height: 20px;">的值,</span><span style="font-size: 12pt; line-height: 20px;">next[2]=0</span><span style="font-size: 12pt; line-height: 20px;">,意思是</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">已经和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">比较过了,不相等,接下来去比较</span><span style="font-size: 12pt; line-height: 20px;">S[2]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">吧。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">假设</span><span style="font-size: 12pt; line-height: 20px;">S=”</span><span style="font-size: 15pt; line-height: 26px;">abaabcabdabba</span><span style="font-size: 12pt; line-height: 20px;">”</span><span style="font-size: 12pt; line-height: 20px;">在</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中搜索</span><span style="font-size: 12pt; line-height: 20px;">T=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">abaabd</span><span style="font-size: 12pt; line-height: 20px;">”呢?答:这种情况当比较到</span><span style="font-size: 12pt; line-height: 20px;">S[5]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[5]</span><span style="font-size: 12pt; line-height: 20px;">时,发现不等,就去看</span><span style="font-size: 12pt; line-height: 20px;">next[5]</span><span style="font-size: 12pt; line-height: 20px;">的值,</span><span style="font-size: 12pt; line-height: 20px;">next[5]=2</span><span style="font-size: 12pt; line-height: 20px;">,意思是前面的比较过了,其中,</span><span style="font-size: 12pt; line-height: 20px;">S[5]</span><span style="font-size: 12pt; line-height: 20px;">的前面有两个字符和</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的开始两个相等,接下来去比较</span><span style="font-size: 12pt; line-height: 20px;">S[5]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[2]</span><span style="font-size: 12pt; line-height: 20px;">吧。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">总之,有了串的</span><span style="font-size: 12pt; line-height: 20px;">next</span><span style="font-size: 12pt; line-height: 20px;">值,一切搞定。那么,怎么求串的模式函数值</span><span style="font-size: 12pt; line-height: 20px;">next[n]</span><span style="font-size: 12pt; line-height: 20px;">呢?(本文中</span><span style="font-size: 12pt; line-height: 20px;">next</span><span style="font-size: 12pt; line-height: 20px;">值、模式函数值、模式值是一个意思。)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><strong><span style="font-size: 16pt; line-height: 27px;">三</span><span style="font-size: 16pt; line-height: 27px;">. </span><span style="font-size: 16pt; line-height: 27px;">怎么求串的模式值</span><span style="font-size: 16pt; line-height: 27px;">next[n]</span></strong></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><strong><span style="font-size: 12pt; line-height: 20px;">定义</span></strong><span style="font-size: 12pt; line-height: 20px;">:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 24pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">(</span><span style="font-size: 12pt; color: black; line-height: 20px;">1</span><span style="font-size: 12pt; color: black; line-height: 20px;">)</span><span style="font-size: 12pt; color: black; line-height: 20px;">next[0]= -1 </span><span style="font-size: 12pt; color: black; line-height: 20px;">意义:任何串的第一个字符的模式值规定为</span><span style="font-size: 12pt; color: black; line-height: 20px;">-1</span><span style="font-size: 12pt; color: black; line-height: 20px;">。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 24pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">(</span><span style="font-size: 12pt; color: black; line-height: 20px;">2</span><span style="font-size: 12pt; color: black; line-height: 20px;">)</span><span style="font-size: 12pt; color: black; line-height: 20px;">next[j]= -1   </span><span style="font-size: 12pt; color: black; line-height: 20px;">意义:模式串</span><span style="font-size: 12pt; color: black; line-height: 20px;">T</span><span style="font-size: 12pt; color: black; line-height: 20px;">中下标为</span><span style="font-size: 12pt; color: black; line-height: 20px;">j</span><span style="font-size: 12pt; color: black; line-height: 20px;">的字符,如果与首字符</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 4.6pt; text-indent: 150pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">相同,且</span><span style="font-size: 12pt; color: black; line-height: 20px;">j</span><span style="font-size: 12pt; color: black; line-height: 20px;">的前面的</span><span style="font-size: 12pt; color: black; line-height: 20px;">1—k</span><span style="font-size: 12pt; color: black; line-height: 20px;">个字符与开头的</span><span style="font-size: 12pt; color: black; line-height: 20px;">1—k</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 4.6pt; text-indent: 150pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">个字符不等(或者相等但</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[k]==T[j]</span><span style="font-size: 12pt; color: black; line-height: 20px;">)(</span><span style="font-size: 12pt; color: black; line-height: 20px;">1</span><span style="font-size: 12pt; color: black; line-height: 20px;">≤</span><span style="font-size: 12pt; color: black; line-height: 20px;">k<j</span><span style="font-size: 12pt; color: black; line-height: 20px;">)。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 4.6pt; text-indent: 150pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">如:</span><span style="font-size: 12pt; color: black; line-height: 20px;">T=”abCabCad” </span><span style="font-size: 12pt; color: black; line-height: 20px;">则</span><span style="font-size: 12pt; color: black; line-height: 20px;"> next[6]=-1</span><span style="font-size: 12pt; color: black; line-height: 20px;">,因</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[3]=T[6]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 24pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">(</span><span style="font-size: 12pt; color: black; line-height: 20px;">3</span><span style="font-size: 12pt; color: black; line-height: 20px;">)</span><span style="font-size: 12pt; color: black; line-height: 20px;">next[j]=k    </span><span style="font-size: 12pt; color: black; line-height: 20px;">意义:模式串</span><span style="font-size: 12pt; color: black; line-height: 20px;">T</span><span style="font-size: 12pt; color: black; line-height: 20px;">中下标为</span><span style="font-size: 12pt; color: black; line-height: 20px;">j</span><span style="font-size: 12pt; color: black; line-height: 20px;">的字符,如果</span><span style="font-size: 12pt; color: black; line-height: 20px;">j</span><span style="font-size: 12pt; color: black; line-height: 20px;">的前面</span><span style="font-size: 12pt; color: black; line-height: 20px;">k</span><span style="font-size: 12pt; color: black; line-height: 20px;">个</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 156pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">字符与开头的</span><span style="font-size: 12pt; color: black; line-height: 20px;">k</span><span style="font-size: 12pt; color: black; line-height: 20px;">个字符相等,且</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[j] != T[k] </span><span style="font-size: 12pt; color: black; line-height: 20px;">(</span><span style="font-size: 12pt; color: black; line-height: 20px;">1</span><span style="font-size: 12pt; color: black; line-height: 20px;">≤</span><span style="font-size: 12pt; color: black; line-height: 20px;">k<j</span><span style="font-size: 12pt; color: black; line-height: 20px;">)。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 164.25pt; text-indent: -138pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">                       </span><span style="font-size: 12pt; color: black; line-height: 20px;">即</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[0]T[1]T[2]</span><span style="font-size: 12pt; color: black; line-height: 20px;">。。。</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[k-1]==</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 147pt; text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">T[j-k]T[j-k+1]T[j-k+2]…T[j-1]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 147pt; text-indent: 30pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">且</span><span style="font-size: 12pt; color: black; line-height: 20px;">T[j] != T[k].</span><span style="font-size: 12pt; color: black; line-height: 20px;">(</span><span style="font-size: 12pt; color: black; line-height: 20px;">1</span><span style="font-size: 12pt; color: black; line-height: 20px;">≤</span><span style="font-size: 12pt; color: black; line-height: 20px;">k<j</span><span style="font-size: 12pt; color: black; line-height: 20px;">)</span><span style="font-size: 12pt; color: black; line-height: 20px;">;</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 164.25pt; text-indent: -138pt; line-height: 18px;"><span style="font-size: 12pt; color: black; line-height: 20px;">(4) next[j]=0   </span><span style="font-size: 12pt; color: black; line-height: 20px;">意义:除(</span><span style="font-size: 12pt; color: black; line-height: 20px;">1</span><span style="font-size: 12pt; color: black; line-height: 20px;">)(</span><span style="font-size: 12pt; color: black; line-height: 20px;">2</span><span style="font-size: 12pt; color: black; line-height: 20px;">)(</span><span style="font-size: 12pt; color: black; line-height: 20px;">3</span><span style="font-size: 12pt; color: black; line-height: 20px;">)的其他情况。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin: 7.5pt 0cm; line-height: 18px;"> </div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><strong><span style="font-size: 12pt; line-height: 20px;">举例</span></strong><span style="font-size: 12pt; line-height: 20px;">:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">01</span><span style="font-size: 12pt; line-height: 20px;">)</span><span style="font-size: 12pt; line-height: 20px;">求</span><span style="font-size: 12pt; line-height: 20px;">T=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">abcac</span><span style="font-size: 12pt; line-height: 20px;">”的模式函数的值。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">     next[0]= -1 </span><span style="font-size: 12pt; line-height: 20px;">根据(</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">     next[1]=0   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (4)   </span><span style="font-size: 12pt; line-height: 20px;">因(</span><span style="font-size: 12pt; line-height: 20px;">3</span><span style="font-size: 12pt; line-height: 20px;">)有</span><span style="font-size: 12pt; line-height: 20px;">1<=k<j;</span><span style="font-size: 12pt; line-height: 20px;">不能说,</span><span style="font-size: 12pt; line-height: 20px;">j=1,T[j-1]==T[0]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">     next[2]=0   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (4)   </span><span style="font-size: 12pt; line-height: 20px;">因(</span><span style="font-size: 12pt; line-height: 20px;">3</span><span style="font-size: 12pt; line-height: 20px;">)有</span><span style="font-size: 12pt; line-height: 20px;">1<=k<j;</span><span style="font-size: 12pt; line-height: 20px;">(</span><span style="font-size: 12pt; line-height: 20px;">T[0]=a</span><span style="font-size: 12pt; line-height: 20px;">)</span><span style="font-size: 12pt; line-height: 20px;">!=</span><span style="font-size: 12pt; line-height: 20px;">(</span><span style="font-size: 12pt; line-height: 20px;">T[1]=b</span><span style="font-size: 12pt; line-height: 20px;">)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">     next[3]= -1 </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (2)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">     next[4]=1   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) T[0]=T[3] </span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;"> T[1]=T[4]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 21.75pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">    </span><span style="font-size: 12pt; line-height: 20px;">即</span><span style="font-size: 12pt; line-height: 20px;">   </span></div><table cellspacing="0" cellpadding="0" width="227" border="1" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51); border: medium none; margin-left: 2cm; width: 6cm; border-collapse: collapse;"><tbody><tr><td valign="top" width="95" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 71pt;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">下标</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td></tr><tr><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">T</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.55pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">c</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">c</span></div></td></tr><tr><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.55pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td></tr></tbody></table><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 48.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">若</span><span style="font-size: 12pt; line-height: 20px;">T=</span><span style="font-size: 12pt; line-height: 20px;">“</span><span style="font-size: 12pt; line-height: 20px;">abcab</span><span style="font-size: 12pt; line-height: 20px;">”将是这样:</span></div><table cellspacing="0" cellpadding="0" width="227" border="1" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51); border: medium none; margin-left: 2cm; width: 6cm; border-collapse: collapse;"><tbody><tr><td valign="top" width="95" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 71pt;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">下标</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td></tr><tr><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">T</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.55pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">c</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td></tr><tr><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.55pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="95" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 71.05pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td></tr></tbody></table><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 60pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">为什么</span><span style="font-size: 12pt; line-height: 20px;">T[0]==T[3],</span><span style="font-size: 12pt; line-height: 20px;">还会有</span><span style="font-size: 12pt; line-height: 20px;">next[4]=0</span><span style="font-size: 12pt; line-height: 20px;">呢</span><span style="font-size: 12pt; line-height: 20px;">, </span><span style="font-size: 12pt; line-height: 20px;">因为</span><span style="font-size: 12pt; line-height: 20px;">T[1]==T[4], </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3)” </span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;">T[j] != T[k]”</span><span style="font-size: 12pt; line-height: 20px;">被划入(</span><span style="font-size: 12pt; line-height: 20px;">4</span><span style="font-size: 12pt; line-height: 20px;">)。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">02</span><span style="font-size: 12pt; line-height: 20px;">)来个复杂点的,求</span><span style="font-size: 12pt; line-height: 20px;">T=”ababcaabc” </span><span style="font-size: 12pt; line-height: 20px;">的模式函数的值。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[0]= -1    </span><span style="font-size: 12pt; line-height: 20px;">根据(</span><span style="font-size: 12pt; line-height: 20px;">1</span><span style="font-size: 12pt; line-height: 20px;">)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 21.75pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">         next[1]=0    </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;">(4)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 21.75pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">         next[2]=-1   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (2)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[3]=0   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) </span><span style="font-size: 12pt; line-height: 20px;">虽</span><span style="font-size: 12pt; line-height: 20px;">T[0]=T[2] </span><span style="font-size: 12pt; line-height: 20px;">但</span><span style="font-size: 12pt; line-height: 20px;">T[1]=T[3] </span><span style="font-size: 12pt; line-height: 20px;">被划入(</span><span style="font-size: 12pt; line-height: 20px;">4</span><span style="font-size: 12pt; line-height: 20px;">)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[4]=2   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) T[0]T[1]=T[2]T[3] </span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;">T[2] !=T[4]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[5]=-1 </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (2) </span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[6]=1   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) T[0]=T[5] </span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;">T[1]!=T[6]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[7]=0   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) </span><span style="font-size: 12pt; line-height: 20px;">虽</span><span style="font-size: 12pt; line-height: 20px;">T[0]=T[6] </span><span style="font-size: 12pt; line-height: 20px;">但</span><span style="font-size: 12pt; line-height: 20px;">T[1]=T[7] </span><span style="font-size: 12pt; line-height: 20px;">被划入(</span><span style="font-size: 12pt; line-height: 20px;">4</span><span style="font-size: 12pt; line-height: 20px;">)</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[8]=2   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) T[0]T[1]=T[6]T[7] </span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;">T[2] !=T[8]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 78.85pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"> </span><span style="font-size: 12pt; line-height: 20px;">即</span></div><table cellspacing="0" cellpadding="0" width="378" border="1" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51); border: medium none; margin-left: 2cm; width: 10cm; border-collapse: collapse;"><tbody><tr><td valign="top" width="57" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 42.6pt;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">下标</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">5</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">6</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">7</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">8</span></div></td></tr><tr><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">T</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">c</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">c</span></div></td></tr><tr><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.6pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="57" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 42.65pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td></tr></tbody></table><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 66pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">只要理解了</span><span style="font-size: 12pt; line-height: 20px;">next[3]=0</span><span style="font-size: 12pt; line-height: 20px;">,而不是</span><span style="font-size: 12pt; line-height: 20px;">=1</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">next[6]=1</span><span style="font-size: 12pt; line-height: 20px;">,而不是</span><span style="font-size: 12pt; line-height: 20px;">= -1</span><span style="font-size: 12pt; line-height: 20px;">,</span><span style="font-size: 12pt; line-height: 20px;">next[8]=2</span><span style="font-size: 12pt; line-height: 20px;">,而不是</span><span style="font-size: 12pt; line-height: 20px;">= 0</span><span style="font-size: 12pt; line-height: 20px;">,其他的好象都容易理解。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin-left: 309.75pt; text-indent: -18pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">03)</span><span style="font-size: 7pt; line-height: 15px;">   </span><span style="font-size: 12pt; line-height: 20px;">来个特殊的,求</span><span style="font-size: 12pt; line-height: 20px;"> T=”abCabCad” </span><span style="font-size: 12pt; line-height: 20px;">的模式函数的值。</span></div><table cellspacing="0" cellpadding="0" width="340" border="1" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51); border: medium none; margin-left: 2cm; width: 9cm; border-collapse: collapse;"><tbody><tr><td valign="top" width="63" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 47.3pt;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">下标</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">5</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">6</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">7</span></div></td></tr><tr><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 47.3pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">T</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">C</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">b</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">C</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">d</span></div></td></tr><tr><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 47.3pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td></tr></tbody></table><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">         </span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 60pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[5]= 0 </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) </span><span style="font-size: 12pt; line-height: 20px;">虽</span><span style="font-size: 12pt; line-height: 20px;">T[0]T[1]=T[3]T[4],</span><span style="font-size: 12pt; line-height: 20px;">但</span><span style="font-size: 12pt; line-height: 20px;">T[2]==T[5]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 60pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[6]= -1 </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (2) </span><span style="font-size: 12pt; line-height: 20px;">虽前面有</span><span style="font-size: 12pt; line-height: 20px;">abC=abC,</span><span style="font-size: 12pt; line-height: 20px;">但</span><span style="font-size: 12pt; line-height: 20px;">T[3]==T[6]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 60pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next[7]=4   </span><span style="font-size: 12pt; line-height: 20px;">根据</span><span style="font-size: 12pt; line-height: 20px;"> (3) </span><span style="font-size: 12pt; line-height: 20px;">前面有</span><span style="font-size: 12pt; line-height: 20px;">abCa=abCa,</span><span style="font-size: 12pt; line-height: 20px;">且</span><span style="font-size: 12pt; line-height: 20px;"> T[4]!=T[7]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 42pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">若</span><span style="font-size: 12pt; line-height: 20px;">T[4]==T[7]</span><span style="font-size: 12pt; line-height: 20px;">,即</span><span style="font-size: 12pt; line-height: 20px;">T=” adCadCad”,</span><span style="font-size: 12pt; line-height: 20px;">那么将是这样:</span><span style="font-size: 12pt; line-height: 20px;">next[7]=0, </span><span style="font-size: 12pt; line-height: 20px;">而不是</span><span style="font-size: 12pt; line-height: 20px;">= 4,</span><span style="font-size: 12pt; line-height: 20px;">因为</span><span style="font-size: 12pt; line-height: 20px;">T[4]==T[7].</span></div><table cellspacing="0" cellpadding="0" width="340" border="1" style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51); border: medium none; margin-left: 2cm; width: 9cm; border-collapse: collapse;"><tbody><tr><td valign="top" width="63" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 47.3pt;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">下标</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">5</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">6</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: 1pt 1pt 1pt medium; border-style: solid solid solid none; padding: 0cm 5.4pt; border-top-color: windowtext; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">7</span></div></td></tr><tr><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 47.3pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">T</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">d</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">C</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">d</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">C</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">a</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">d</span></div></td></tr><tr><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt; border-style: none solid solid; padding: 0cm 5.4pt; border-left-color: windowtext; width: 47.3pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">next</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">-1</span></div></td><td valign="top" width="63" style="border-right-color: windowtext; border-width: medium 1pt 1pt medium; border-style: none solid solid none; padding: 0cm 5.4pt; width: 47.35pt; border-bottom-color: windowtext;"><div align="left" style="line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">0</span></div></td></tr></tbody></table><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin: 7.5pt 0cm; line-height: 18px;"> </div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); margin: 7.5pt 0cm; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">如果你觉得有点懂了,那么</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 6pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">练习:求</span><span style="font-size: 12pt; line-height: 20px;">T=”AAAAAAAAAAB” </span><span style="font-size: 12pt; line-height: 20px;">的模式函数值,并用后面的求模式函数值函数验证。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><strong><span style="font-size: 12pt; line-height: 20px;">意义</span></strong><span style="font-size: 12pt; line-height: 20px;">:</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 6pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;"> next </span><span style="font-size: 12pt; line-height: 20px;">函数值究竟是什么含义,前面说过一些,这里总结。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 6pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">设在字符串</span><span style="font-size: 12pt; line-height: 20px;">S</span><span style="font-size: 12pt; line-height: 20px;">中查找模式串</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">,若</span><span style="font-size: 12pt; line-height: 20px;">S[m]!=T[n],</span><span style="font-size: 12pt; line-height: 20px;">那么,取</span><span style="font-size: 12pt; line-height: 20px;">T[n]</span><span style="font-size: 12pt; line-height: 20px;">的模式函数值</span><span style="font-size: 12pt; line-height: 20px;">next[n],</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">1.</span><span style="font-size: 7pt; line-height: 15px;">       </span><span style="font-size: 12pt; line-height: 20px;">next[n]= -1 </span><span style="font-size: 12pt; line-height: 20px;">表示</span><span style="font-size: 12pt; line-height: 20px;">S[m]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">间接比较过了,不相等,下一次比较</span><span style="font-size: 12pt; line-height: 20px;"> S[m+1] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">2.</span><span style="font-size: 7pt; line-height: 15px;">       </span><span style="font-size: 12pt; line-height: 20px;">next[n]=0 </span><span style="font-size: 12pt; line-height: 20px;">表示比较过程中产生了不相等,下一次比较</span><span style="font-size: 12pt; line-height: 20px;"> S[m] </span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[0]</span><span style="font-size: 12pt; line-height: 20px;">。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">3.</span><span style="font-size: 7pt; line-height: 15px;">       </span><span style="font-size: 12pt; line-height: 20px;">next[n]= k >0 </span><span style="font-size: 12pt; line-height: 20px;">但</span><span style="font-size: 12pt; line-height: 20px;">k<n, </span><span style="font-size: 12pt; line-height: 20px;">表示</span><span style="font-size: 12pt; line-height: 20px;">,S[m]</span><span style="font-size: 12pt; line-height: 20px;">的前</span><span style="font-size: 12pt; line-height: 20px;">k</span><span style="font-size: 12pt; line-height: 20px;">个字符与</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">中的开始</span><span style="font-size: 12pt; line-height: 20px;">k</span><span style="font-size: 12pt; line-height: 20px;">个字符已经间接比较相等了,下一次比较</span><span style="font-size: 12pt; line-height: 20px;">S[m]</span><span style="font-size: 12pt; line-height: 20px;">和</span><span style="font-size: 12pt; line-height: 20px;">T[k]</span><span style="font-size: 12pt; line-height: 20px;">相等吗?</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">4.</span><span style="font-size: 7pt; line-height: 15px;">       </span><span style="font-size: 12pt; line-height: 20px;">其他值,不可能。</span></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><strong><span style="font-size: 16pt; line-height: 27px;">四</span><span style="font-size: 16pt; line-height: 27px;">. </span><span style="font-size: 16pt; line-height: 27px;">求串</span><span style="font-size: 16pt; line-height: 27px;">T</span><span style="font-size: 16pt; line-height: 27px;">的模式值</span><span style="font-size: 16pt; line-height: 27px;">next[n]</span><span style="font-size: 16pt; line-height: 27px;">的函数</span></strong></div><div align="left" style="font-family: Arial; font-size: 14px; color: rgb(51, 51, 51); text-indent: 24pt; line-height: 18px;"><span style="font-size: 12pt; line-height: 20px;">说了这么多,是不是觉得求串</span><span style="font-size: 12pt; line-height: 20px;">T</span><span style="font-size: 12pt; line-height: 20px;">的模式值</span><span style="font-size: 12pt; line-height: 20px;">next[n]</span><span style="font-size: 12pt; line-height: 20px;">很复杂呢?要叫我写个函数出来,目前来说,我宁愿去登天。好在有现成的函数,当初发明</span><span style="font-size: 12pt; line-height: 20px;">KMP</span><span style="font-size: 12pt; line-height: 20px;">算法,写出这个函数的先辈,令我佩服得六体投地。我等后生小子,理解起来,都要反复琢磨。下面是这个函数</span><span style="font-size: 12pt; line-height: 20px;">:</span></div><pre name="code" class="cpp">void get_nextval(const char *T, int next[])  {         // 求模式串T的next函数值并存入数组 next。         int j = 0, k = -1;         next[0] = -1;         while ( T[j/*+1*/] != '/0' )         {                if (k == -1 || T[j] == T[k])                {                       ++j; ++k;                       if (T[j]!=T[k])                              next[j] = k;                       else                              next[j] = next[k];                }// if                else                       k = next[k];         }// while      ////这里是我加的显示部分     // for(int i=0;i<j;i++)         //{         //     cout<<next[i];         //}         //cout<<endl;  }// get_nextval   

另一种写法,也差不多。

void getNext(const char* pattern,int next[])  {         next[0]=   -1;         int k=-1,j=0;         while(pattern[j] != '/0')         {                if(k!= -1 && pattern[k]!= pattern[j] )                       k=next[k];                ++j;++k;                if(pattern[k]== pattern[j])                       next[j]=next[k];                else                       next[j]=k;         }         ////这里是我加的显示部分     // for(int i=0;i<j;i++)         //{         //     cout<<next[i];         //}         //cout<<endl;  }

下面是KMP模式匹配程序,各位可以用他验证。记得加入上面的函数

#include <iostream.h>  #include <string.h>  int KMP(const char *Text,const char* Pattern) //const 表示函数内部不会改变这个参数的值。  {         if( !Text||!Pattern|| Pattern[0]=='/0' || Text[0]=='/0' )//                return -1;//空指针或空串,返回-1。         int len=0;         const char * c=Pattern;         while(*c++!='/0')//移动指针比移动下标快。         {                    ++len;//字符串长度。         }         int *next=new int[len+1];         get_nextval(Pattern,next);//求Pattern的next函数值              int index=0,i=0,j=0;         while(Text[i]!='/0' && Pattern[j]!='/0' )         {                if(Text[i]== Pattern[j])                {                       ++i;// 继续比较后继字符                       ++j;                }                else                {                       index += j-next[j];                       if(next[j]!=-1)                              j=next[j];// 模式串向右移动                       else                       {                              j=0;                              ++i;                       }                }         }//while              delete []next;         if(Pattern[j]=='/0')                return index;// 匹配成功         else                return -1;        }  int main()//abCabCad  {         char* text="bababCabCadcaabcaababcbaaaabaaacababcaabc";      char*pattern="adCadCad";         //getNext(pattern,n);      //get_nextval(pattern,n);        cout<<KMP(text,pattern)<<endl;         return 0;  }

五.其他表示模式值的方法
上面那种串的模式值表示方法是最优秀的表示方法,从串的模式值我们可以得到很多信息,以下称为第一种表示方法。第二种表示方法,虽然也定义next[0]= -1,但后面绝不会出现-1,除了next[0],其他模式值next[j]=k(0k<j)的意义可以简单看成是:下标为j的字符的前面最多k个字符与开始的k个字符相同,这里并不要求T[j] != T[k]。其实next[0]也可以定义为0(后面给出的求串的模式值的函数和串的模式匹配的函数,是next[0]=0的),这样,next[j]=k(0k<j)的意义都可以简单看成是:下标为j的字符的前面最多k个字符与开始的k个字符相同。第三种表示方法是第一种表示方法的变形,即按第一种方法得到的模式值,每个值分别加1,就得到第三种表示方法。第三种表示方法,我是从论坛上看到的,没看到详细解释,我估计是为那些这样的编程语言准备的:数组的下标从1开始而不是0
 下面给出几种方法的例子:
      表一。
下标
0
1
2
3
4
5
6
7
8
T
a
b
a
b
c
a
a
b
c
(1) next
-1
0
-1
0
2
-1
1
0
2
(2) next
-1
0
0
1
2
0
1
1
2
(3) next
0
1
0
1
3
0
2
1
3
第三种表示方法,在我看来,意义不是那么明了,不再讨论。
           表二。
下标
0
1
2
3
4
T
a
b
c
A
c
(1)next
-1
0
0
-1
1
(2)next
-1
0
0
0
1
      表三。
下标
0
1
2
3
4
5
6
7
T
a
d
C
a
d
C
a
d
(1)next
-1
0
0
-1
0
0
-1
0
(2)next
-1
0
0
0
1
2
3
4
 
对比串的模式值第一种表示方法和第二种表示方法,看表一:
第一种表示方法next[2]= -1,表示T[2]=T[0],且T[2-1] !=T[0]
第二种表示方法next[2]= 0,表示T[2-1] !=T[0],但并不管T[0] T[2]相不相等。
第一种表示方法next[3]= 0,表示虽然T[2]=T[0],但T[1] ==T[3]
第二种表示方法next[3]= 1,表示T[2] =T[0],他并不管T[1] T[3]相不相等。
第一种表示方法next[5]= -1,表示T[5]=T[0],且T[4] !=T[0]T[3]T[4] !=T[0]T[1]T[2]T[3]T[4] !=T[0]T[1]T[2]
第二种表示方法next[5]= 0,表示T[4] !=T[0]T[3]T[4] !=T[0]T[1] T[2]T[3]T[4] !=T[0]T[1]T[2],但并不管T[0] T[5]相不相等。换句话说:就算T[5]==’x’, T[5]==’y’,T[5]==’9’,也有next[5]= 0 
从这里我们可以看到:串的模式值第一种表示方法能表示更多的信息,第二种表示方法更单纯,不容易搞错。当然,用第一种表示方法写出的模式匹配函数效率更高。比如说,在串S=adCadCBdadCadCad 9876543”中匹配串T=adCadCad用第一种表示方法写出的模式匹配函数,当比较到S[6] != T[6] 时,取next[6]= -1(表三),它可以表示这样许多信息:S[3]S[4]S[5]==T[3]T[4]T[5]==T[0]T[1]T[2],而S[6] != T[6]T[6]==T[3]==T[0],所以S[6] != T[0],接下来比较S[7]T[0]吧。如果用第二种表示方法写出的模式匹配函数,当比较到S[6] != T[6] 时,取next[6]= 3(表三),它只能表示:S[3]S[4]S[5]== T[3]T[4]T[5]==T[0]T[1]T[2],但不能确定T[6]T[3]相不相等,所以,接下来比较S[6]T[3];又不相等,取next[3]= 0,它表示S[3]S[4]S[5]== T[0]T[1]T[2],但不会确定T[3]T[0]相不相等,即S[6]T[0] 相不相等,所以接下来比较S[6]T[0],确定它们不相等,然后才会比较S[7]T[0]。是不是比用第一种表示方法写出的模式匹配函数多绕了几个弯。
为什么,在讲明第一种表示方法后,还要讲没有第一种表示方法好的第二种表示方法?原因是:最开始,我看严蔚敏的一个讲座,她给出的模式值表示方法是我这里的第二种表示方法,如图:
她说:“next 函数值的含义是:当出现S[i] !=T[j]时,下一次的比较应该在S[i]T[next[j]] 之间进行。”虽简洁,但不明了,反复几遍也没明白为什么。而她给出的算法求出的模式值是我这里说的第一种表示方法next值,就是前面的get_nextval()函数。匹配算法也是有瑕疵的。于是我在这里发帖说她错了:
http://community.csdn.net/Expert/topic/4413/4413398.xml?temp=.2027246
   现在看来,她没有错,不过有张冠李戴之嫌。我不知道,是否有人第一次学到这里,不参考其他资料和明白人讲解的情况下,就能搞懂这个算法(我的意思是不仅是算法的大致思想,而是为什么定义和例子中next[j]=k(0k<j),而算法中next[j]=k(-1k<j))。凭良心说:光看这个讲座,我就对这个教受十分敬佩,不仅讲课讲得好,声音悦耳,而且这门课讲得层次分明,恰到好处。在KMP这个问题上出了点小差错,可能是编书的时候,在这本书上抄下了例子,在那本书上抄下了算法,结果不怎么对得上号。因为我没找到原书,而据有的网友说,书上已不是这样,也许吧。说起来,教授们研究的问题比这个高深不知多少倍,哪有时间推演这个小算法呢。总之,瑕不掩玉。
书归正传,下面给出我写的求第二种表示方法表示的模式值的函数,为了从S的任何位置开始匹配T,“当出现S[i] !=T[j]时,下一次的比较应该在S[i]T[next[j]] 之间进行。”    定义next[0]=0 
void myget_nextval(const char *T, int next[])  {       // 求模式串T的next函数值(第二种表示方法)并存入数组 next。                       int j = 1, k = 0;       next[0] = 0;         while ( T[j] != '/0' )       {                         if(T[j] == T[k])                     {                           next[j] = k;                           ++j; ++k;                                      }                     else if(T[j] != T[0])                     {                    next[j] = k;                    ++j;                             k=0;                     }                     else                     {                            next[j] = k;                    ++j;                               k=1;                     }       }//while      for(int i=0;i<j;i++)       {              cout<<next[i];       }       cout<<endl;  }// myget_nextval 

下面是模式值使用第二种表示方法的匹配函数(next[0]=0

int my_KMP(char *S, char *T, int pos)  {  int i = pos, j = 0;//pos(S 的下标0≤pos<StrLength(S))  while ( S[i] != '/0' && T[j] != '/0' )  {      if (S[i] == T[j] )       {           ++i;               ++j; // 继续比较后继字符       }     else             // a b a b c a a b c                      // 0 0 0 1 2 0 1 1 2     {              //-1 0 -1 0 2 -1 1 0 2        i++;       j = next[j];     /*当出现S[i] !=T[j]时,               下一次的比较应该在S[i]和T[next[j]] 之间进行。要求next[0]=0。 在这两个简单示范函数间使用全局数组next[]传值。*/     }  }//while  if ( T[j] == '/0' )      return (i-j); // 匹配成功  else       return -1;  } // my_KMP  









0 0
原创粉丝点击