| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3245 人关注过本帖
标题:[收集]]关于KMP算法!
取消只看楼主 加入收藏
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
 问题点数:0 回复次数:5 
[收集]]关于KMP算法!
关于KMP.老实说,我是看了至少四遍才基本理解它的整个推导过程,真是菜也.
第一遍连看下去的耐心都没有,第二遍看着看着干脆去吃饭了,直到第三遍,第四遍,做了练习之后,情况才有所好转...呵呵..

个人理解KMP的主要思想就是 : 模式串在跟主串匹配的时候主串的指针不回溯。至于它的推导过程,很多书上都有详细过程,我就不多说了,这里给出我写的一个代码,代码有注释,应该能看明白吧!也请大家把自已写过的关于模式匹配的代码共享出来.让我对这个算法的应用有更多的了解,谢谢!!!
my code:



/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) [url]http://[/url] **
*****************************************************************/
#include<iostream>      
#define MAXSIZE 32   
using namespace std;   
//定义结构体   
struct stack_ch   
{   
   
char ch[MAXSIZE];   
    int Length;   
};   
   
//求next函数值   
   
void GetNext(stack_ch ch,int next[MAXSIZE])   
{   
   
int i=1,j=0;   
    next[1]=0;   
    while (i<ch.Length)   
    {   
        
if (j==0 || ch.ch[i]==ch.ch[j])   
        {   
            
i++;   
            j++;   
            next[i]=j;   
        }   
        
else   
            
j=next[j];   
    }   
}
//Get_next   
   
//输入字符串   
void Enter_stack(stack_ch &ch,int next[MAXSIZE])   
{   
        
char data;   
        ch.Length=0;   
        cin>>data;   
        while (data!='*')   
        {   
        
ch.ch[++ch.Length]=data;   
        cin>>data;   
        }   
}   
   
//将模式串和主串进行匹配操作,返回模式串在主串S第pos个字符之后可匹配的位置   
   
int Index_KMP(stack_ch S,stack_ch T,int pos,int next[MAXSIZE])   
{   
        
//利用模式串T的next函数求T在主串S中第pos个字符之后的位置,T非空且1<=pos<=S.Length   
        
int i=pos,j=1;   
        while (i<=S.Length && j<=T.Length)   //对两串扫描
        
{   
        
if (j==0 || S.ch[i]==T.ch[j])    //对应字符匹配
        
{   
            
i++;   
            j++;   
        }   
        
else   
            
j=next[j];   
        }   
        
if (j>T.Length)     
        return (i-T.Length);        //匹配成功
        
else   
        return
0;                 //匹配失败
} //Index_KMP   
   
int main(void)   
{   
        
stack_ch S,T;   
        int next[MAXSIZE],pos,pos2;   
        cout<<"输入主串:(end by '*')"<<endl;   
   
        //输入主串   
   
    Enter_stack(S,next);   
        cout<<"输入模式串:(end by '*')"<<endl;   
   
        //输入模式串   
   
    Enter_stack(T,next);   
        GetNext(T,next);   
        cout<<"输入pos的值(1<=pos<=S.Length)S是主串:"<<endl;   
   
        //输入pos值   
        
cin>>pos;   
        pos2=Index_KMP(S,T,pos,next);   
   
        //输出所求的位置值   
        
cout<<"The position is:"<<pos2<<endl;   
        system("pause");
        return 0;
}


[ 本帖最后由 zjl138 于 2008-5-24 18:09 编辑 ]
搜索更多相关主题的帖子: KMP 算法 收集 模式 代码 
2008-05-24 12:25
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
原帖由 雨中飞燕 于 2008-5-24 14:23 发表 [url=http://bbs.bccn.net/redirect.php?goto=findpost&pid=1282622&ptid=215672]呃。。。这代码。。。。

 

怎么了?
还有个问题,好像你那个高亮软件在论坛升级后失灵了..

[ 本帖最后由 zjl138 于 2008-5-24 14:30 编辑 ]

i like linux...
2008-05-24 14:29
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
好,谢谢!

i like linux...
2008-05-24 14:32
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
sun兄好强!头像又换了,呵呵.........
KMP还有很多应用的,写法也有几种.比如用模式串的首字符与尾字串与主串的相对应位置比较,还有带通配符的.不过我没那么多时间去研究,毕意还有新的课程要赶.很快又要期考了.

i like linux...
2008-05-25 11:21
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
谢谢大家,我改一下.
wait a moment !!!

i like linux...
2008-05-28 15:26
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
请大家点评,谢谢!!!

/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) [url]http://[/url] **
*****************************************************************/

#include<iostream>      
using namespace std;   
const int MAXSIZE=32;
//#define MAXSIZE 32
//定义结构体   
struct stack_ch   
{   
   
char ch[MAXSIZE];   
    int Length;   
};   
   
//求next函数值   
   
void GetNext(stack_ch &ch,int m,int *next,int n)   
{   
   
int i=1,j=0;   
    next[1]=0;   
    while (i<ch.Length)   
    {   
        
if (j==0 || ch.ch[i]==ch.ch[j])   
        {   
            
i++;   
            j++;   
            next[i]=j;   
        }   
        
else   
            
j=next[j];   
    }   
}
//Get_next   
   
//输入字符串   
void Enter_stack(stack_ch &ch,int *next,int m)   
{   
        
char data;   
        ch.Length=0;   
        cin>>data;   
        while (data!='*')   
        {   
        
ch.ch[++ch.Length]=data;   
        cin>>data;   
        }   
}   
   
//将模式串和主串进行匹配操作,返回模式串在主串S第pos个字符之后可匹配的位置   
   
int Index_KMP(stack_ch S,stack_ch T,int pos,int *next,int m)   
{   
        
//利用模式串T的next函数求T在主串S中第pos个字符之后的位置,T非空且1<=pos<=S.Length   
        
int i=pos,j=1;   
        while (i<=S.Length && j<=T.Length)   //对两串扫描
        
{   
        
if (j==0 || S.ch[i]==T.ch[j])    //对应字符匹配
        
{   
            
i++;   
            j++;   
        }   
        
else   
            
j=next[j];   
        }   
        
if (j>T.Length)     
        return (i-T.Length);        //匹配成功
        
else   
        return
0;                 //匹配失败
} //Index_KMP   
   
int main(void)   
{   
        
stack_ch S,T;   
        int next[MAXSIZE]={0},pos,pos2;   
        cout<<"输入主串:(end by '*')"<<endl;   
   
        //输入主串   
        
Enter_stack(S,next,MAXSIZE);   
        cout<<"输入模式串:(end by '*')"<<endl;   
   
        //输入模式串   
        
Enter_stack(T,next,MAXSIZE);   
        GetNext(T,MAXSIZE,next,MAXSIZE);   
        cout<<"输入pos的值(1<=pos<=S.Length)S是主串:"<<endl;   
   
        //输入pos值   
        
cin>>pos;   
        pos2=Index_KMP(S,T,pos,next,MAXSIZE);   
   
        //输出所求的位置值   
        
cout<<"The position is:"<<pos2<<endl;   
        system("pause");
        return 0;
}   
   

i like linux...
2008-05-28 15:33
快速回复:[收集]]关于KMP算法!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.083508 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved