zoukankan      html  css  js  c++  java
  • Chapter 5(串)

    1.kmp
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    
    
    
    void get_nextval(char *str,int *nextval)
    {
    	int i,j;
    	i = 0;
    	j = -1;
    	nextval[0] = -1;
    
    
    	int len = strlen(str);
    	while(i < len) 
    	{
    		if(j==-1 || str[j]==str[i])//str[i]表示后缀的单个字符,str[j]表示前缀的单个字符
    		{
    			++i;
    			++j;
    			if(str[i] != str[j]) //若当前字符与前缀字符不同,则当前的j为nextvale在i位置的值
    			{
    				nextval[i] = j;
    			}
    			else //否则将前缀字符的nextval值赋给后缀字符的nextval
    			{
    				nextval[i] = nextval[j];
    			}
    		}
    		else
    		{
    			j = nextval[j];//若字符不同,则j值回溯
    		}
    	}
    }
    
    
    void get_next(char *str,int *next)
    {
    	int i,j;
    	i = 0;
    	j = -1;
    	next[0] = -1;
    
    	int len = strlen(str);
    	while(i < len)
    	{
    		if(j==-1 || str[i]==str[j])
    		{
    			++i;
    			++j;
    			next[i] = j;
    		}
    		else
    		{
    			j = next[j];
    		}
    	}
    }
    
    
    
    int Index_KMP(char *strF,char *strS,int pos)
    {
    	int i = pos;
    
    	int j = 0;//字串中当前位置下标值
    	int next[255];
    	get_nextval(strS,next);
    	
    	int lenF = strlen(strF);
    	int lenS = strlen(strS);
    	while(i < lenF && j < lenS) //若i小于lenF且j小于lenS,循环继续
    	{
    		if(j == -1 || strF[i]==strS[j])//两字符相等或者j为-1则继续
    		{
    			++i;
    			++j;
    		}
    		else
    		{
    			j = next[j];//j回退合适的位置,i不变
    		}
    	}
    
    	if(j >= lenS)return i-lenS;//必须要由j使循环退出才说明找到了这个子串的位置
    	else return 0;
    }
    
    
    
    int main()
    {
    	char *strF = "abcdecdefg";
    	char *strS = "defg";
    	printf("%d 
    ",Index_KMP(strF,strS,0));
    	return 0;
    }

    附件列表

    • 相关阅读:
      VUE-周日历的实现
      Mac电脑部分网页打不开怎么办
      python pytesseracct WinError2
      Windows Error的错误代码
      python pytesseract——3步识别验证码的识别入门
      python——pyinstaller踩的坑 UnicodeDecodeError
      python 处理excel踩过的坑——data_only,公式全部丢失
      Python3行代码之——截图工具
      python openpyxl内存不主动释放 ——关闭Excel工作簿后内存依旧(MemoryError)
      pywintypes.com_error: (-2147352567, '发生意外 解决方案
    • 原文地址:https://www.cnblogs.com/LyndonMario/p/9326345.html
    Copyright © 2011-2022 走看看