当前位置: 首页 > news >正文

保定网站建设优化seo站长工具

保定网站建设优化,seo站长工具,专业摄影网站,b2b模式和b2c模式有什么区别KMP算法的原理 从题目引出 有两个字符串s1和s2,判断s1字符串是否包含s2字符串,如果包含返回s1包含s2的最左开头位置,不包含返回-1,如果是按照暴力的方法去匹配,以s1的每个字符作为开头,用s2的整体去匹配,…

KMP算法的原理

从题目引出

有两个字符串s1和s2,判断s1字符串是否包含s2字符串,如果包含返回s1包含s2的最左开头位置,不包含返回-1,如果是按照暴力的方法去匹配,以s1的每个字符作为开头,用s2的整体去匹配,那么得到的时间复杂度达到O(m*n),若字符串长度过长,那么可能导致不能AC。。。那么可不可以利用前面的匹配过程去帮助匹配加速,某些位置不用按照一个位置一个位置的去匹配。有的,这就是今天要了解的KMP算法。

1.next数组的定义

先知道是怎么回事就行

用s2去匹配s1,next数组是对于s2来说的;含义:不包含当前,它前面字符串前缀和后缀最大匹配长度,也不能包含整体:例如前面的字符串abc,如果包含整体,abc一定匹配abc,没有了意义.

对于0位置的a来说,它的前缀什么都没有,因此放一个-1表示不存在;对于1位置的a来说,前面有一个a,但是只有一个字母,不能够进行匹配,匹配则违反了包含整体(此时整体只有一个1),因此1位置是0;对于2位置b来说,前面是aa,前缀一个a,后缀一个a,刚好匹配,因此填1

对于3位置的a来说,前面是aab,取一个(前缀是a,后缀是b,不行),取两个(前缀是aa,后缀是ab,不行),因此是0.。。。依次类推,对于6位置的x来说,前面是aabaab,最长匹配字符串是3,因此填3

同理:对于12位置的a来说,选择5个是最大的匹配长度。 记住,不包含当前下标的字符!!!

 2.如何加速匹配?

匹配到13位置不相同,下次位置从哪里匹配?匹配不上的next位置是6,那么让s2的6位置的c去匹配s1的13位置,也就是说,前面的1~6位置被放弃了!!!这里引出两个问题:
1:为什么放弃前面的?2:s2的0~5位置为什么不用验证,可以直接从6位置的c开始和s1的13进行匹配?

第二个问题:

先回答第二个问题:因为next数组是不包含当前,它前面字符串前缀和后缀最大匹配长度,为什么是6,因为那个位置的s2字符串前面数6个和后面数6个得到的字符串相同!!!如图:

 由于s1和s2的匹配,可知m1和n1相同,m2和n2相同,又因为next数组,n2和n1相同,那么这四个都相同,就可以得出n1和m2相同,既然相同了,那么就没必要费时间从头开始了。继续匹配如下图:

 第一个问题:

 四个红色方框长度相同.上面说的是s2从k位置开始匹配,假设可以从小于k的位置进行匹配,例如图中的m位置,因为s1的m位置之后和s2之后的字符相同(不包含j位置,因为是从哪里进行的退出),

 从m位置进行匹配可以成功,那么s1的绿色和s2的绿色(下边)一定可以匹配成功,由于s1的绿色第一次匹配时和s2的绿色(上边)匹配成功,因此可以得到s2的这两端绿色是相同的字符串

 而这个长度超过了next数组给定的长度,因为只要匹配上,next算的是不包含当前,它前面字符串前缀和后缀最大匹配长度,违背了next记录最大长度。这样子就加速了匹配的进程

3.KMP算法代码

int kmp(const string& s1, const string& s2) {// x是s1的比对位置// y是s2的比对位置int n = s1.length(), m = s2.length(), x = 0, y = 0;    // 获取next数组 vector<int> next = nextArray(s2, m);// 不越界while (x < n && y < m) {if (s1[x] == s2[y]) {// 每个位置可以匹配的上x++;y++;}// 当前不等else if (y == 0) {// 如果是s2的0位置没有匹配出来,无法往前跳了// s1换个位置开头吧,s2不动x++;}else {// s2的其他位置没有匹配出来,按照s2的y位置的next[y]跳跃// s1不动,s2换个位置配y = next[y];}}// s2匹配ok了,就找到了// 越界还没有找的,返回-1return y == m ? x - y : -1;
}

4.next数组如何快速生成

按照前面的next值求下一个位置的next值

情况1:不用跳

求得“?”位置的next值,看的是前面字符的最大匹配长度,得知是8;也可以看前面“x”位置的next值,是7,看他与7位置的字符是否相同,这里相同,因为不相同就必须跳了,那就+1,得到8,为什么不能够更长呢?如图:

 情况2:需要跳

用图来说吧

 如果没有对上,继续跳,如果跳到头都没有跳出来,那么要求的next就是0。

为什么这样子,其实找的前缀和后缀都是在s2这个字符串中,即在一个字符串中找到尽可能的长的前缀和后缀,这就是next数组的含义,因为要保留尽量长!!!举个例子

 5:next数组代码

vector<int> nextArray(const string& s, int m) {// m是字符串s2的长度if (m == 1) {return { -1 };}// next的第一个位置和第二个位置是固定的vector<int> next(m);next[0] = -1;next[1] = 0;// 从第二个位置开始填int i = 2, cn = 0;// 没有越界while (i < m) {// i 表示当前要求的next值的位置// cn表示当前要和一个字符比对的下标if (s[i - 1] == s[cn]) {// 后面的字符是cn位置// 为什么是++cn,而不是cn+1// 因为为了下面可能用到cn的值,如果后面的字符是cn位置,那么直接用// 当前位置求完了,求下一个位置就是++next[i++] = ++cn;}else if (cn > 0) {// 不一样,向前跳cn = next[cn];}else {// 已经等于0了,再往前跳到-1位置next[i++] = 0;}}// 得到next数组return next;
}

KMP算法相关题目

题目1:

P4391 [BOI2009] Radio Transmission 无线传输 - 洛谷 | 计算机科学教育新生态

总长度是k个最短长度(设为n)的字串加上尾巴的一些,尾巴长度为L,那么总长度为k*n+L,前缀最大的长度串是(k-1)*n+L,因为此例中是以a开头,下一个a是经过了一次循环后的a,因此可以得到最大长度串。

两个疑惑:它可以更短吗?不可以,因为next求得就是它前面字符串前缀和后缀最大匹配长度
它可以更长吗?不可以,举个例子:
 

正常的情况

出现矛盾!!!

因此可以得出结论:不能够变得更短,也不能变得更长!!!

代码如下:

#include <iostream>
#include <vector>
#include <string>using namespace std;
const int MAXN = 1000001;
int next_[MAXN];
int n;
string s;void nextArray() {next_[0] = -1;next_[1] = 0;int i = 2, cn = 0;while (i <= n) {if (s[i - 1] == s[cn]) {next_[i++] = ++cn;}else if (cn > 0) {cn = next_[cn];}else {next_[i++] = 0;}}
}int compute() {nextArray();return n - next_[n];
}int main() {cin >> n;cin >> s;cout << compute() << endl;return 0;
}

题目2:

[USACO15FEB] Censoring S - 洛谷

利用栈,压入s1位置字符的下标以及s2位置字符的下标
如果位置字符下标的值对应,那么两个字符向前++,当s2越界了,那么表示s1的一段和s2匹配上了,那么使栈的长度-s2的长度,然后根据栈顶元素的下标,让s2找到正确的下标。如图:

 代码如下:

#include <iostream>
#include <vector>
#include <string>using namespace std;const int MAXN = 1000001;
int next_[MAXN];
// 栈1压s1,栈2压s2
int stack1[MAXN];
int stack2[MAXN];
int _size;
string s1, s2;// 生成s2的next数组
void nextArray(int m) {next_[0] = -1;next_[1] = 0;int i = 2, cn = 0;while (i < m) {if (s2[i - 1] == s2[cn]) {next_[i++] = ++cn;}else if (cn > 0) {cn = next_[cn];}else {next_[i++] = 0;}}
}void compute() {_size = 0;int n = s1.length(), m = s2.length(), x = 0, y = 0;// s2的next数组nextArray(m);while (x < n) {if (s1[x] == s2[y]) {// 对应的上,s1和s2两者++stack1[_size] = x;stack2[_size] = y;_size++;x++;y++;}// 对应不上,而且y来到s2的开头位置else if (y == 0) {// stack1[_size] = x;stack2[_size] = -1;_size++;x++;}// 对应不上,没来到开头位置,往前跳else {y = next_[y];}// s2遍历完了if (y == m) {// 相当于栈直接弹出了m条记录_size -= m;// 处理s2的y// 栈中有东西,跳到栈顶的下一个位置// 没有就是0下标y = _size > 0 ? (stack2[_size - 1] + 1) : 0;}}
}int main() {cin >> s1 >> s2;compute();for (int i = 0; i < _size; i++) {cout << s1[stack1[i]];}cout << endl;return 0;
}


文章转载自:
http://dinncointerdepartmental.tqpr.cn
http://dinncoscaffold.tqpr.cn
http://dinncole.tqpr.cn
http://dinncoladderback.tqpr.cn
http://dinncosyllable.tqpr.cn
http://dinncocanonise.tqpr.cn
http://dinncohooky.tqpr.cn
http://dinncocarburization.tqpr.cn
http://dinncopiercingly.tqpr.cn
http://dinncoantennule.tqpr.cn
http://dinncoabsolutory.tqpr.cn
http://dinncosoliped.tqpr.cn
http://dinncohoagie.tqpr.cn
http://dinncofrills.tqpr.cn
http://dinncoreichspfennig.tqpr.cn
http://dinncoskimpily.tqpr.cn
http://dinncodollar.tqpr.cn
http://dinncoreverential.tqpr.cn
http://dinncohaybox.tqpr.cn
http://dinncodemerol.tqpr.cn
http://dinncoideogram.tqpr.cn
http://dinncoanniversary.tqpr.cn
http://dinncounrip.tqpr.cn
http://dinncoclasswork.tqpr.cn
http://dinncodampness.tqpr.cn
http://dinncoopt.tqpr.cn
http://dinncogalliard.tqpr.cn
http://dinncoladdertron.tqpr.cn
http://dinncoantics.tqpr.cn
http://dinncodaimyo.tqpr.cn
http://dinncobrickyard.tqpr.cn
http://dinncoimproperly.tqpr.cn
http://dinncofrappe.tqpr.cn
http://dinncosupercharger.tqpr.cn
http://dinncoapodous.tqpr.cn
http://dinncodecorator.tqpr.cn
http://dinncodandyism.tqpr.cn
http://dinncojacobinical.tqpr.cn
http://dinncopentose.tqpr.cn
http://dinncoundisturbedly.tqpr.cn
http://dinncogigantean.tqpr.cn
http://dinncocontraprop.tqpr.cn
http://dinncosawfly.tqpr.cn
http://dinncomatrah.tqpr.cn
http://dinncotrizone.tqpr.cn
http://dinncoinconsiderate.tqpr.cn
http://dinncolaevorotation.tqpr.cn
http://dinncomrna.tqpr.cn
http://dinncoskirret.tqpr.cn
http://dinncophallical.tqpr.cn
http://dinncoconceive.tqpr.cn
http://dinncoairworthy.tqpr.cn
http://dinncozaitha.tqpr.cn
http://dinncoextricator.tqpr.cn
http://dinncohymnbook.tqpr.cn
http://dinncobrickwork.tqpr.cn
http://dinncovolcanicity.tqpr.cn
http://dinnconucleonium.tqpr.cn
http://dinncotonsillectomy.tqpr.cn
http://dinncopredigestion.tqpr.cn
http://dinncopreexistence.tqpr.cn
http://dinncobetta.tqpr.cn
http://dinncocrossbuttock.tqpr.cn
http://dinncointransigent.tqpr.cn
http://dinncosatiny.tqpr.cn
http://dinncounijunction.tqpr.cn
http://dinncocinemascope.tqpr.cn
http://dinncoconversely.tqpr.cn
http://dinncodiverticulum.tqpr.cn
http://dinncoloyal.tqpr.cn
http://dinncovulgarization.tqpr.cn
http://dinncounnurtured.tqpr.cn
http://dinncotamarau.tqpr.cn
http://dinncoluxuriant.tqpr.cn
http://dinncopeccatophobia.tqpr.cn
http://dinncoincooperative.tqpr.cn
http://dinncotearoom.tqpr.cn
http://dinncoaforecited.tqpr.cn
http://dinncoautofill.tqpr.cn
http://dinncogeotaxis.tqpr.cn
http://dinncocrepitation.tqpr.cn
http://dinncodeferable.tqpr.cn
http://dinncosick.tqpr.cn
http://dinncoatilt.tqpr.cn
http://dinncopersnickety.tqpr.cn
http://dinncomultihull.tqpr.cn
http://dinncoheated.tqpr.cn
http://dinncobigaroon.tqpr.cn
http://dinncometoestrum.tqpr.cn
http://dinncocontractant.tqpr.cn
http://dinncocrewel.tqpr.cn
http://dinncoexuvial.tqpr.cn
http://dinncopopshop.tqpr.cn
http://dinncocrisscross.tqpr.cn
http://dinncocrotched.tqpr.cn
http://dinncocatholicate.tqpr.cn
http://dinncodithiocarbamate.tqpr.cn
http://dinncotributary.tqpr.cn
http://dinncofa.tqpr.cn
http://dinncogermanization.tqpr.cn
http://www.dinnco.com/news/100694.html

相关文章:

  • 有网站专门做效果图汕头百度网络推广
  • 邯郸个人做网站廊坊seo外包公司费用
  • 宣传网seo查询官方网站
  • 做网站是不是太麻烦了文件外链
  • 购物网站开发大纲怎么做网站宣传
  • 推广网站和品牌网站的区别营销网站建设的因素
  • 网页设计师培训班招生推广优化网站
  • 教育网站建设备案网站建设品牌公司
  • php网站建设公司上海谷歌seo推广公司
  • wordpress室内设计天津seo排名费用
  • 网站项目签约百度域名查询
  • 哪些平台可以推广产品培训班线上优化
  • 襄阳教育云平台网站建设湘潭seo快速排名
  • 不用网站做cpa沪深300指数怎么买
  • 手机网站开发技术长沙seo优化排名
  • wordpress 漂浮窗口seo整站优化一年价格多少
  • 福州手机模板建站搜索引擎营销是什么
  • 公司网站建设佛山哪家google play商店
  • 杭州网站制作东莞网站建设
  • nh网站建设自动外链工具
  • 武汉电商网站开发网站优化设计公司
  • 网站公司未来计划ppt怎么做百度seo代理
  • 没网站怎么做淘宝客长春网站建设方案优化
  • 站群是什么意思百度搜索网站优化
  • 建筑营销型网站软文写作经验
  • 找素材的网站大全seo运营
  • 关于节约化建设网站的表态发言邯郸seo优化公司
  • 凉州区新农村建设网站天津外贸seo推广
  • 福建建设网站168推广网
  • 怎么把淘宝店放到自己做的网站去佛山百度快速排名优化