刘伟 发表于 2006-4-15 16:19

[原创]发一道数据结构上机题(串的匹配)

<strong><font size="3">&nbsp;</font></strong> <p align="justify">int Index-BF ( SString S, SString T, <b>int</b> pos ) <b><font lang="ZH-CN" face="宋体" size="3">{</font></b></p><font size="3"><p align="justify">// <font lang="ZH-CN" face="宋体" size="3">串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">和</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">均采用定长顺序存储结构,</font><i><font size="3">S </font></i> <font lang="ZH-CN" face="宋体" size="3">和</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">存放串长度。</font></p></font><font lang="ZH-CN" face="宋体" size="3">串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">和</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">均采用定长顺序存储结构,</font><i><font size="3">S </font></i> <font lang="ZH-CN" face="宋体" size="3">和</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">存放串长度。</font><font size="3"><p align="justify">// <font lang="ZH-CN" face="宋体" size="3">返回模式</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">在主串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">中第</font><font size="3"> <i>pos</i> </font><font lang="ZH-CN" face="宋体" size="3">个字符之后的位置,如果不存在,</font></p></font><font lang="ZH-CN" face="宋体" size="3">返回模式</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">在主串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">中第</font><font size="3"> <i>pos</i> </font><font lang="ZH-CN" face="宋体" size="3">个字符之后的位置,如果不存在,</font><font size="3"><p align="justify">// <font lang="ZH-CN" face="宋体" size="3">则返回函数值</font><font size="3"> 0</font><font lang="ZH-CN" face="宋体" size="3">。其中</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">非空,</font><font size="3">1</font><font lang="ZH-CN" face="宋体" size="3">≤</font><i><font size="3">pos</font></i><font lang="ZH-CN" face="宋体" size="3">≤</font><i><font size="3">S-<i>T</i>+1</font></i>-<i>T</i>+1<font lang="ZH-CN" face="宋体" size="3">。</font></p></font><font lang="ZH-CN" face="宋体" size="3">则返回函数值</font><font size="3"> 0</font><font lang="ZH-CN" face="宋体" size="3">。其中</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">非空,</font><font size="3">1</font><font lang="ZH-CN" face="宋体" size="3">≤</font><i><font size="3">pos</font></i><font lang="ZH-CN" face="宋体" size="3">≤</font><i><font size="3">S-<i>T</i>+1</font></i>-<i>T</i>+1<font lang="ZH-CN" face="宋体" size="3">。</font><font size="3"><p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><font size="3">i = pos; j = 1;<p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">从主串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">的第</font><font size="3"> <i>pos</i> </font><font lang="ZH-CN" face="宋体" size="3">个字符起和模式</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">的第一个字符比较</font></p></font></p></font><font lang="ZH-CN" face="宋体" size="3"></font><font size="3">i = pos; j = 1;<p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">从主串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">的第</font><font size="3"> <i>pos</i> </font><font lang="ZH-CN" face="宋体" size="3">个字符起和模式</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">的第一个字符比较</font></p></font><font lang="ZH-CN" face="宋体" size="3"></font><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">从主串</font><font size="3"> <i>S</i> </font><font lang="ZH-CN" face="宋体" size="3">的第</font><font size="3"> <i>pos</i> </font><font lang="ZH-CN" face="宋体" size="3">个字符起和模式</font><font size="3"> <i>T</i> </font><font lang="ZH-CN" face="宋体" size="3">的第一个字符比较</font><font size="3"><p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">while ( i &lt;= S <b>&amp;&amp;</b> j &lt;= T ) </font></b> ( i &lt;= S <b>&amp;&amp;</b> j &lt;= T ) <b><font lang="ZH-CN" face="宋体" size="3">{</font></b><font size="3"> <p align="justify">i<b>f </b>( S = = T ) <b><font lang="ZH-CN" face="宋体" size="3">{</font><font size="3"> ++i; ++j; </font></b>++i; ++j; <b><font lang="ZH-CN" face="宋体" size="3">} </font></b><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">若字符相等,则继续比较后继字符</font></p></font></p></font><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">while ( i &lt;= S <b>&amp;&amp;</b> j &lt;= T ) </font></b> ( i &lt;= S <b>&amp;&amp;</b> j &lt;= T ) <b><font lang="ZH-CN" face="宋体" size="3">{</font></b><font size="3"> <p align="justify">i<b>f </b>( S = = T ) <b><font lang="ZH-CN" face="宋体" size="3">{</font><font size="3"> ++i; ++j; </font></b>++i; ++j; <b><font lang="ZH-CN" face="宋体" size="3">} </font></b><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">若字符相等,则继续比较后继字符</font></p></font><b><font lang="ZH-CN" face="宋体" size="3">{</font><font size="3"> ++i; ++j; </font></b>++i; ++j; <b><font lang="ZH-CN" face="宋体" size="3">} </font></b><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">若字符相等,则继续比较后继字符</font><font size="3"><p align="justify"><b>else</b> <b><font lang="ZH-CN" face="宋体" size="3">{</font><font size="3"> i = i – j + 2; j = 1; </font></b>i = i – j + 2; j = 1; <b><font lang="ZH-CN" face="宋体" size="3">} </font></b><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">若字符不等,则指针后退重新开始匹配</font></p></font><b><font lang="ZH-CN" face="宋体" size="3">{</font><font size="3"> i = i – j + 2; j = 1; </font></b>i = i – j + 2; j = 1; <b><font lang="ZH-CN" face="宋体" size="3">} </font></b><font size="3">// </font><font lang="ZH-CN" face="宋体" size="3">若字符不等,则指针后退重新开始匹配</font><font size="3"><p align="justify"><font lang="ZH-CN" face="宋体" size="3"><b>}</b></font><font size="3"> // while </font><font lang="ZH-CN" face="宋体" size="3">结束</font></p></font><font lang="ZH-CN" face="宋体" size="3"><b>}</b></font><font size="3"> // while </font><font lang="ZH-CN" face="宋体" size="3">结束</font><font size="3"><p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">if ( j &gt; T ) <b>return</b> i–T;<p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">else return 0;</font></b> 0;</p></font></b> ( j &gt; T ) <b>return</b> i–T;</p></font><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">if ( j &gt; T ) <b>return</b> i–T;<p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">else return 0;</font></b> 0;</p></font></b> ( j &gt; T ) <b>return</b> i–T;<p align="justify"><font lang="ZH-CN" face="宋体" size="3"></font><b><font size="3">else return 0;</font></b> 0;</p><b><font lang="ZH-CN" face="宋体" size="3"><p align="justify">}<font size="3"><font face="宋体, MS Song"> // Index-BF</font></font></p></font></b><font size="3"><font face="宋体, MS Song"> // Index-BF</font></font><div><br/> </div><div><pre>//Index_BF.cpp



#include &lt;stdio.h&gt;

#include &lt;conio.h&gt;

#include &lt;iostream.h&gt;

#define MAXSTRLEN 255



typedef unsigned char SString;                    //from 0 to 256 becoz of '\0'



int Index_BF(SString S,SString T,int pos)                      //Index_BF() function

{

    int i=pos,j=1;                                             //from the pos char in main SString on

        while(i&lt;=S&amp;&amp;j&lt;=T)

        { if(S==T)

                {++i;

             ++j;

                }

           else

                { i=i-j+2;

              j=1;

                }

        }// end of while

        if(j&gt;T) return (i-T);

    else return (0);

}//end of Index_BF() function





void main()                                                     //main() function

{

   SString S,T;

   int pos;                                                     //  1&lt;=pos&lt;=S-T+1;

   int m,n,r;                                                   //  the length of SString S and T

   cout&lt;&lt;"Index_BF.cpp"&lt;&lt;endl&lt;&lt;"============"&lt;&lt;endl&lt;&lt;endl;

   cout&lt;&lt;"Please input the Main SString S: &lt;eg. student&gt; ";

   cin.getline(S+1,MAXSTRLEN);

   cout&lt;&lt;"Please input the SString T : &lt;eg. den&gt; ";

   cin.getline(T+1,MAXSTRLEN);

   cout&lt;&lt;"From ? on: &lt;eg. 1&gt; ";

   cin&gt;&gt;pos;



   for(m=0;S!='\0';m++);                                      // get the length of SString S

   S=m-1;

   for(n=0;T!='\0';n++);                                      // get the length of SString T

   T=n-1;



   if(r=Index_BF(S,T,pos))                                

          cout&lt;&lt;endl&lt;&lt;"The postion of T in the Main SString S is: "&lt;&lt;r&lt;&lt;endl&lt;&lt;"...OK...!"&lt;&lt;endl;

   else cout&lt;&lt;"Failure to Index the SString!"&lt;&lt;endl;

   getch();

}//end of main() function

</pre></div>

newbrightness7 发表于 2006-4-20 17:01

<p>好,学习一下,谢谢楼主!</p>

qswb 发表于 2006-6-18 22:06

<p>有很多种算法呢</p><p>如bmp、bm</p>

xjtu_yu 发表于 2006-6-20 13:54

好的,谢谢

gssdzc 发表于 2010-6-16 20:18

感谢楼主。。。。。

ywzbismarck 发表于 2011-4-25 13:09

好,学习一下,谢谢楼主!

发表于 1970-1-1 08:00

页: [1]
查看完整版本: [原创]发一道数据结构上机题(串的匹配)