• 模式匹配-KMP算法


    /***字符串匹配算法***/
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -2
    typedef int Status;
    #define MAXSTRLEN 255   		//用户可在255以内定义最长串长
    typedef char SString[MAXSTRLEN+1];		//0号单元存放串的长度
    
    Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
    	int i;
    	if (strlen(chars) > MAXSTRLEN)
    		return ERROR;
    	else {
    		T[0] = strlen(chars);
    		for (i = 1; i <= T[0]; i++)
    			T[i] = *(chars + i - 1);
    		return OK;
    	}
    }
    //算法4.3 计算next函数值
    void get_next(SString T, int next[])
    { //求模式串T的next函数值并存入数组next
    	int i = 1, j = 0;
    	next[1] = 0;
    	while (i < T[0])
    		if (j == 0 || T[i] == T[j])
    		{
    			++i;
    			++j;
    			next[i] = j;
    		}
    		else
    			j = next[j];
    }//get_next
    
    //算法4.2 KMP算法
    int Index_KMP(SString S, SString T, int pos, int next[])
    { 	// 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
    	//当中,T非空。1≤pos≤StrLength(S)
    	int i = pos, j = 1;
    	while (i <= S[0] && j <= T[0])
    		if (j == 0 || S[i] == T[j]) // 继续比較后继字
    		{
    			++i;
    			++j;
    		}
    		else
    			j = next[j]; // 模式串向右移动
    	if (j > T[0]) // 匹配成功
    		return i - T[0];
    	else
    		return 0;
    }//Index_KMP
    
    int main()
    {
    	SString S;
    	StrAssign(S,"aaabbaba") ;
    	SString T;
    	StrAssign(T,"abb") ;
    	int *p = new int[T[0]+1]; // 生成T的next数组
    	get_next(T,p);
    	cout<<"主串和子串在第"<<Index_KMP(S,T,1,p)<<"个字符处首次匹配
    ";
    	return 0;
    }

  • 相关阅读:
    时间差的计算
    时间差的计算第二篇
    并不能完全不编码完成Windows Service的安装和卸载
    今年是搜索引擎年吗?热!搜索引擎算法点击率火爆
    Virtual PC,我真的不敢用你!
    我理解的17种C#写的Hello World程序
    反搜索引擎
    如何保证Windows Serverice健壮长效运转?
    服务器是怎么做成的?
    超酷的超级DataGrid
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5206722.html
Copyright © 2020-2023  润新知