text2-25.htm
来自「浙江大学计算机学院数据结构课程的教学课件」· HTM 代码 · 共 92 行
HTM
92 行
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#3399FF" alink="#FF0066">
<div id="Layer1" style="position:absolute; width:711px; height:21px; z-index:1; top: 10px; background-color: #CCCCCC; layer-background-color: #CCCCCC; border: 1px none #000000; left: 26px"><b>|</b><font face="宋体" size="2"><a href="../text1/text1-0.htm">第一章</a></font><b>|</b><font face="宋体" size="2">第二章</font><b>|</b><font face="宋体" size="2"><a href="../text3/text3-0.htm">第三章</a></font><b>|</b><font face="宋体" size="2"><a href="../text4/text4-0.htm">第四章</a></font><b>|</b><font face="宋体" size="2"><a href="../text5/text5-0.htm">第五章</a></font><b>|</b><font face="宋体" size="2"><a href="../text6/text6-0.htm">第六章</a></font><b>|</b><font face="宋体" size="2"><a href="../text7/text7-0.htm">第七章</a></font><b>|</b><font face="宋体" size="2"><a href="../text8/text8-0.htm">第八章</a></font><b>|</b><font face="宋体" size="2"><a href="../text9/text9-0.htm">第九章</a></font><b>|</b><font face="宋体" size="2"><a href="../text10/text10-0.htm">第十章</a></font><b>|</b><font size="2" face="宋体"><a href="../textA/textA-0.htm">算法分析</a><b><font color="#000000">|</font></b>
</font></div>
<pre align="left"><b><font face="Arial" size="4">
#include <stdio.h><stdio.h>
#include <string.h><string.h>
#define max_sring_size 100
#define max_pattern_size 100
int <font color="#FF0033">pmatch</font> ( );
void <font color="#FF0033">fail</font> ( );
int failure [ max_pattern_size ];
char pat [ max_pattern_size ];
char string [ max_string_size ];
_______________________________________________________
int <font color="#FF0033">pmatch</font> ( char *string, char *pat)
{ <i><font face="Arial, Helvetica, sans-serif" size="3" color="#CC0099">/* Knuth, Morris, Pratt string matching algorithm , O(strlen(string)) */
</font></i><font face="Arial, Helvetica, sans-serif" size="3" color="#CC0099"><font size="4" color="#000000">int i = 0, j = 0;</font></font>
int lens = strlen ( string );
int lenp = strlen ( pat);
while ( i < lens && j < lenp ) {
if (string [ i ] == pat [ j ] ) {
i ++; j ++}
else if ( j == 0 ) i ++; <i><font face="Arial, Helvetica, sans-serif" size="3" color="#CC0099"> /* no match, s i+1~p1*/</font></i>
else j = <font color="#FF0099">failure</font> [ j - 1 ] + 1;<i><font face="Arial, Helvetica, sans-serif" size="3" color="#CC0099"> /* j-1 symbols match, si+1 ~p f[j-1]+1*/</font></i>
}
return (( j == lenp ) ? ( i - lenp ) : -1 );
}
<i> <font color="#FF0033">i</font>
</i> s = <i> ' - a b c a ? ? . . . ? '</i>
pat = <i> ' a b c a b c a c a b ' </i><i>
<font color="#FF0033">j</font>
<font color="#FF0033">j </font> 0 1 2 3 4 5 6 7 8 9
<font color="#FF0033"> f </font> <font color="#0033CC">-1 -1 -1 0 1 2 3 -1 0 1</font></i>
_________________________________________________________
<font color="#FF0033">computting the failure function</font> <i>O(strlen(pat))</i>
void<font color="#FF0033"> fail</font> ( (char *pat )
(<font size="3"><i><font color="#FF0099"> /* compute the pattern's failure function */</font></i></font>
int n = strlen (pat );
failure [ 0 ] = -1;
for ( j = 1; j < n; j ++) {
i = <font color="#FF0099">failure</font> [ j -1 ];
while ( ( pat [ j ] != pat [ i +1 ] && ( i >= 0))
i =<font color="#FF0099"> failure</font> [ i ];
if ( pat [ j ] == pat [ i + 1] )
<font color="#FF0099">failure</font> [ j ] == i + 1;
else <font color="#FF0099"> failure </font>[ j ] = -1;
}
} </font></b></pre>
<div align="left"></div>
<table width="633" align="left" cellspacing="0" bordercolor="#FFFFFF">
<tr>
<td width="76"> </td>
<td width="133"><font face="Arial, Helvetica, sans-serif" size="4" color="#FF0000"><b>-1
</b></font></td>
<td width="416"><b><font face="Arial" size="4"><i> if j =0</i></font></b></td>
</tr>
<tr>
<td width="76"><b><font face="Arial" size="4"><i> f (j) = </i></font></b></td>
<td width="133"><font face="Arial, Helvetica, sans-serif" size="4" color="#FF0000"><b><img src="image/fm.gif" width="102" height="23"></b></font></td>
<td width="416"><b><font face="Arial" size="4"><i> where m is the least integer
k for which <img src="image/pm.gif" width="124" height="24"></i></font></b></td>
</tr>
<tr>
<td width="76"> </td>
<td width="133"><font face="Arial, Helvetica, sans-serif" size="4" color="#FF0000"><b>-1</b></font></td>
<td width="416"><b><font face="Arial" size="4"><i>if there is no k satisfying
the above</i></font></b></td>
</tr>
</table>
<pre align="left"><b><font face="Arial" size="4"> <font color="#FF0033">
exercises: page 77/ 2,6 page 80/ 3 page91/ 9 page 97/ 10</font>
</font></b></pre>
<table width="731" cellspacing="0" cellpadding="0">
<tr>
<td width="327"> </td>
<td width="271"><a href="../index.htm"><img width="60" height="25" usemap="#MapMap4" border="0" src="../../images/home.gif"></a><a href="../index.htm"><map name="MapMap4"><area shape="rect" coords="42,-34,88,-15" href="text0.htm"><area shape="rect" coords="4,4,55,23" href="text2-index.htm"></map></a></td>
<td width="131"><font face="楷体_GB2312" size="2"><b><a href="text2-24.htm">上一页</a></b></font></td>
</tr>
</table>
<p> </p></body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?