串的模式匹配算法 ------ KMP算法
//KMP串的模式匹配算法
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int* get_next(char t[], int length)
{
int i = , j = -;
int* next = (int *)malloc(length * sizeof(int));
next[] = -;
while (i < length)
{
if (j == - || t[i] == t[j])
{
i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
} return next;
} int Index_KMP(char s[], char t[], int sl, int tl, int next[])
{
int j, i;
j = ;
i = ;
while (j < tl && i < sl)
{
if (j == - || s[i] == t[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j == tl)
{
return i - tl;
}
else
{
return ;
}
} //计算next函数修正值的算法
int *get_nextval(char t[], int length)
{
int i = , j = -;
int* nextval = (int*)malloc(length*sizeof(int));
nextval[] = -;
while (i < length)
{
if (j == - || t[i]==t[j])
{
j++;
i++;
if (t[j]!=t[i])
{
nextval[i] = j;
}
else
{
nextval[i] = nextval[j]; //回溯到下标为nextval[j]但元素与下标为nextval[i]的元素不相同的位置
}
}
else
{
j = nextval[j];
}
} return nextval;
} int main()
{ char s[] = "acabaabaabcacaabc";
char t[] = "abaabcac";
char t2[] = "abcdabd";
int tl = strlen(t);
int sl = strlen(s);
/*int *next = get_next(t, tl);
int c = Index_KMP(s, t, sl, tl, next); //返回开始正确匹配的下标(s的下标)
printf("%d\n", c);*/
int l = strlen(t2);
int * nextval = get_nextval(t,tl);
/*for(int i = 0; i<l;i++)
{
printf("%d ",nextval[i]);
}*/
int c = Index_KMP(s, t, sl, tl, nextval);
printf("%d\n",c); printf("\n");
return ;
}
理解:
模式匹配就是将主串中下标为i的元素与模式串中下标为j的元素进行比较(比较过程中i不会回溯 而j的值会按照next对应的值进行回溯)
串的模式匹配算法 ------ KMP算法的更多相关文章
- 《数据结构》之串的模式匹配算法——KMP算法
//串的模式匹配算法 //KMP算法,时间复杂度为O(n+m) #include <iostream> #include <string> #include <cstri ...
- 串的模式之kmp算法实践题
给定两个由英文字母组成的字符串 String 和 Pattern,要求找到 Pattern 在 String 中第一次出现的位置,并将此位置后的 String 的子串输出.如果找不到,则输出“Not ...
- Java数据结构之字符串模式匹配算法---KMP算法2
直接接上篇上代码: //KMP算法 public class KMP { // 获取next数组的方法,根据给定的字符串求 public static int[] getNext(String sub ...
- Java数据结构之字符串模式匹配算法---KMP算法
本文主要的思路都是参考http://kb.cnblogs.com/page/176818/ 如有冒犯请告知,多谢. 一.KMP算法 KMP算法可以在O(n+m)的时间数量级上完成串的模式匹配操作,其基 ...
- 数据结构- 串的模式匹配算法:BF和 KMP算法
数据结构- 串的模式匹配算法:BF和 KMP算法 Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...
- 【算法】串的模式匹配算法(KMP)
串的模式匹配算法 问题: 求子串位置的定位函数如何写? int index(SString S,SString T,int pos); 给定串S,子串T,问T在 ...
- 串、串的模式匹配算法(子串查找)BF算法、KMP算法
串的定长顺序存储#define MAXSTRLEN 255,//超出这个长度则超出部分被舍去,称为截断 串的模式匹配: 串的定义:0个或多个字符组成的有限序列S = 'a1a2a3…….an ' n ...
- 字符串匹配算法——KMP算法
处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...
- 串的应用与kmp算法讲解--学习笔记
串的应用与kmp算法讲解 1. 写作目的 平时学习总结的学习笔记,方便自己理解加深印象.同时希望可以帮到正在学习这方面知识的同学,可以相互学习.新手上路请多关照,如果问题还请不吝赐教. 2. 串的逻辑 ...
随机推荐
- P3727 曼哈顿计划E
点分治+SG函数还真是令人意外的组合啊 思路 这道题看到找一条满足条件的链,想到点分治 看到博弈,想到SG函数 然后就变成一道SG函数+点分治的题了 然后1e9的SG函数怎么搞?当然是打表了 然后各种 ...
- SalGAN: Visual saliency prediction with generative adversarial networks
SalGAN: Visual saliency prediction with generative adversarial networks 2017-03-17 摘要:本文引入了对抗网络的对抗训练 ...
- 5、iptables之nat
iptables: 显式扩展.网络防火墙 显式扩展:multiport, iprange, string, time, connlimit, limit, state state:无关是哪种协议 /p ...
- 【译】第38节---EF6-基于代码的配置
原文:http://www.entityframeworktutorial.net/entityframework6/code-based-configuration.aspx EF6引入了基于代码的 ...
- 转载]SAP囚徒 - 通过销售订单领用到成本中心,FI替代
转载]SAP囚徒 - 通过销售订单领用到成本中心,FI替代 原文地址:SAP囚徒 - 通过销售订单领用到成本中心,FI替代实现不同成本中心记账科目不同作者:SAP囚徒 物品领用通常是库存管理的范畴 ...
- [UVA-11039]Children's Game
解析 微扰法贪心经典题 代码 #include <bits/stdc++.h> using namespace std; bool cmp(const string &x, con ...
- 新建DataTable添加列添加行
新建空Table添加行和列 DataTable dt = new DataTable(); //创建空DataTable 1.添加列 dt.Columns.Add("序号", ty ...
- 【Selenium2】【项目实战】
[public/login.py] from selenium import webdriverfrom selenium.webdriver.common.by import Byimport ti ...
- 【Django】【二】模板
1. Django-bootstrap3 guest>python -m pip install django-bootstrap3 [代码] settings.py ""& ...
- ImgNoGoodWindow
using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...