Pattern Matching
朴素的模式匹配算法
设主串S长度为n,子串T长度为m。
对于主串的每个字符,做长度为$strlen(T)$的循环,判断是否与子串匹配。
最好的情况就是一开始就匹配成功,时间复杂度O(1);
最坏的情况就是每次匹配失败都是在T的最后一个元素,复杂度O(n*m);
平均情况复杂度O(n + m)。
int match(string s, string t) {
if (t.size() > s.size())
return -; int i = , j = ;
while (i < s.size() && j < t.size()) {
if (t[j] == s[i]) {
++i;
++j;
}
else {
i = i - j + ;
j = ;
}
} if (j == t.size())
return i - j;
else
return -;
}
KMP算法
这一步关键在于得到Next数组,从T的第一位开始对自身匹配,在某一位置能匹配的最长长度即是当前位置Next值。
这步的匹配和朴素匹配没有太大差异,只是主串S的指针不用回溯,而将子串的指针j回溯到Next[j]位置。
void nextCompute(string t, vector<int>& next) {
int i = , j = -;
while (i < t.size()) {
if (j == - || t[i] == t[j]) {
++i;
++j;
next[i] = j;
}
else {
j = next[j];
}
}
} int KMP(string s, string t) {
vector<int> next(t.size() + , -);
nextCompute(t, next); int i = , j = ;
while (i < (int)s.size() && j < (int)t.size()) {
if (j == - || s[i] == t[j]) {
++i;
++j;
}
else {
j = next[j];
}
} if (j == t.size())
return i - j;
else
return -;
}
改进KMP算法
主要改进了Next数组。
/*计算next数组*/
void next_compute(char T[], int* next)
{
int i = , j = -;
next[] = -;
while (i < strlen(T))
{
if (- == j || T[i] == T[j]) //自匹配
{
i++;
j++;
if (T[i] != T[j])
next[i] = j;
else
next[i] = next[j];
}
else //字符不同,j值回溯
{
j = next[j];
}
}
}
Pattern Matching的更多相关文章
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- scala pattern matching
scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...
- Zhu-Takaoka Two-dimensional Pattern Matching
Two dimensional pattern matching. Details may be added later.... Corresponding more work can be foun ...
- [PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching
Pattern matching in functional programming languages is a way to break up expressions into individua ...
- [Scala] Pattern Matching(模式匹配)
Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...
- pattern matching is C# 7.0
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is 原来的版本 private static s ...
- C#9.0 终于来了,带你一起解读Pattern matching 和 nint 两大新特性玩法
一:背景 1. 讲故事 上一篇跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和公号里的阅读量都达到了新高,甚是欣慰,不管大家对新特性是多 ...
- 函数式编程之-模式匹配(Pattern matching)
模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式表达式长下面的样子: match [somethi ...
- KMP string pattern matching
The function used here is from the leetcode. Details can be found in leetcode problem: Implement str ...
随机推荐
- escape和unescape知识点
decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码. encodeURI() 函数可把字符串作为 URI 进行编码 <script> var uri= ...
- flaks-自定义url转换器
flaks-自定义url转换器 from flask import Flask, url_for from werkzeug.routing import BaseConverter app = Fl ...
- C语言 文件操作(四)
1.fprintf int fprintf(FILE *stream, const char *format, ...) stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流 ...
- Web前端必备-Nginx知识汇总
一.Nginx简介 Nginx是一个高性能.轻量级的Web和反向代理服务器, 其特点是占有内存及资源少.抗并发能力强. Nginx安装简单.配置简洁.启动快速便捷.支持热部署.支持 SSL.拥有高度模 ...
- CARS: 华为提出基于进化算法和权值共享的神经网络结构搜索,CIFAR-10上仅需单卡半天 | CVPR 2020
为了优化进化算法在神经网络结构搜索时候选网络训练过长的问题,参考ENAS和NSGA-III,论文提出连续进化结构搜索方法(continuous evolution architecture searc ...
- python3(二十三)classInstance
""" 类和实例和访问权限 """ __author__ = 'shaozhiqi' # class后面紧接着是类名,即Student,类名 ...
- 双色球的Python实现
代码如下: red_ball = [] blue_ball = [] count = 0 while count < 6: n = int(input('\033[31mPlease enter ...
- Git应用详解第七讲:Git refspec与远程分支的重要操作
前言 前情提要:Git应用详解第六讲:Git协作与Git pull常见问题 这一节来介绍本地仓库与远程仓库的分支映射关系:git refspec.彻底弄清楚本地仓库到底是如何与远程仓库进行联系的. 一 ...
- ElasticSearch 常用查询语句
为了演示不同类型的 ElasticSearch 的查询,我们将使用书文档信息的集合(有以下字段:title(标题), authors(作者), summary(摘要), publish_date(发布 ...
- 数据结构和算法(Golang实现)(19)排序算法-冒泡排序
冒泡排序 冒泡排序是大多数人学的第一种排序算法,在面试中,也是问的最多的一种,有时候还要求手写排序代码,因为比较简单. 冒泡排序属于交换类的排序算法. 一.算法介绍 现在有一堆乱序的数,比如:5 9 ...