HDU 1711(KMP)字符串匹配
KMP 算法
我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习;
字符串匹配 就是KMP,一般思想,用一个for循环找开头 如果开头一样进入第二个for循环;这样统计 直观 容易理解。但是时间复杂度比较长。所以就有KMP 这个算法了
KMP 大体思想因为在上一个方法之中,有的字符重复比较了,使得找到目标字符窜效率比较低,
比如 :目标串为: ① a b a b c
在 ② a b a a b a b c d a b c d b c 这个字符窜中找到上述目标串;
如用 i,j 来表示①,②两个字符串的下标,假设下标从一开始;
i=1,j=1 a=a;
i=2,j=2 b=b;
i=3,j=3 a=a;
i=4;j=4 b!=a; 此时如果只将①字符串右移动一位,虽然可以;但是,假如你移动两位看看怎么样?
a b a b c
a b a a b a b c d a b c d b c
是不是觉得移动2格对得非常整齐?
那么为什么移动两个?
第3个a与第一个a正好重复!所以我们知道重复单元就知道移动多少。需要一个数组记录当两个字符不相等,i 应该指向哪个!记录重复单元的值,叫做部分匹配值!不懂可以百度部分匹配值!以上就是KMP 核心思想
下面贴一个代码!!两种写法!//屏蔽是另一种写法
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- int N[1000005],M[10005],a[10005],n,m;
- void set_a()
- {
- int i,j;
- a[1]=0;
- j=1;
- for(i=2; i<=m; i++)
- {
- if(M[j]==M[i])
- {
- a[i]=j;
- j++;
- }
- else if(M[j]!=M[i]&&M[i]==M[1])
- {
- a[i]=1;
- j=2;
- }
- else
- {
- a[i]=0;
- j=1;
- }
- }
- // for(i=1; i<=m; i++)
- // printf("%d ",a[i]);
- // printf("\n");
- }
- int kmp()
- {
- int i=1,j=1,k;
- set_a();
- while(i<=n)
- {
- if(j==1&&N[i]!=M[j]) i++;
- if(N[i]==M[j])
- {
- i++;
- j++;
- }
- else j=a[j-1]+1;
- if(j==m+1) return i-m;
- }
- return -1;
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--)
- {
- //int b; scanf("%d",&b);
- int i;
- scanf("%d%d",&n,&m);
- memset(a,0,sizeof(a));
- for(i=1; i<=n; i++)
- scanf("%d",&N[i]);
- for(i=1; i<=m; i++)
- scanf("%d",&M[i]);
- printf("%d\n",kmp());
- }
- return 0;
- }
- ///**********************KMP****************************/
- //
- //#include<stdio.h>
- //#include<string.h>
- //using namespace std;
- //int lena,lenb;
- //int a[1000005],b[10005];
- //int next[200000];
- //void set_naxt()//子串的next数组
- //{
- // int i=0,j=-1;
- // next[0]=-1;
- // while(i<lenb)
- // {
- // if(j==-1||b[i]==b[j])
- // {
- // i++;
- // j++;
- // next[i]=j;
- // }
- // else
- // j=next[j];
- // }
- // // printf(">>%d\n",next[lenb-1]);
- // for(i=1; i<=lenb; i++)
- // printf("%d ",next[i]);
- // printf("\n");
- //}
- //
- //int kmp()
- //{
- // int i=0,j=0;
- // set_naxt();
- // while(i<lena)
- // {
- // if(j==-1||a[i]==b[j])
- // {
- // i++;
- // j++;
- // }
- // else
- // j=next[j];
- // if(j==lenb)
- // return i-j+1;
- // }
- // return -1;
- //}
- //int main()
- //{
- // int i,t;
- // scanf("%d",&t);
- // while(t--)
- // {
- // memset(next,0,sizeof(next));
- // scanf("%d%d",&lena,&lenb);
- // for(i=0; i<lena; i++)
- // scanf("%d",&a[i]);
- // for(i=0; i<lenb; i++)
- // scanf("%d",&b[i]);
- // printf("%d\n",kmp());
- // }
- //}
HDU 1711(KMP)字符串匹配的更多相关文章
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 洛谷P3375 - 【模板】KMP字符串匹配
原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...
- Luogu 3375 【模板】KMP字符串匹配(KMP算法)
Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...
- 洛谷P3375 [模板]KMP字符串匹配
To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...
- P3375 【模板】KMP字符串匹配
P3375 [模板]KMP字符串匹配 https://www.luogu.org/problemnew/show/P3375 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在 ...
- 洛谷—— P3375 【模板】KMP字符串匹配
P3375 [模板]KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如 ...
- P3375 模板 KMP字符串匹配
P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...
- KMP字符串匹配 模板 洛谷 P3375
KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...
- KMP字符串匹配学习
KMP字符串匹配学习 牛逼啊 SYC大佬的博客
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
随机推荐
- Arduino学习笔记1---开发环境搭建
主要内容:(一). Arduino IDE的下载及安装 (二). Arduino IDE的应用 (三). Arduino的程序结构 (四). Arduino程序的编译及下载 (一). Arduino ...
- 树莓派用gobot测试舵机的使用
package main import ( "gobot.io/x/gobot" "gobot.io/x/gobot/drivers/gpio" "g ...
- linux svn配置hooks
先创建仓库: svnadmin create /data/svn/my.com 再配置权限: #cd /data/svn/my.com/conf/ #vim svnserve.conf 配置 [gen ...
- CentOS 5.4 final下Systemtap的安装
CentOS 5.4 final下Systemtap的安装 时间:2015-02-11来源:linux网站 作者:zklth 一.Systemtap运行环境需求 (1)linux kernel ...
- 管理weblogic服务的启动和停止
2012-11-10 12:58 26036人阅读 评论(4) 收藏 举报 分类: WebLogic(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 介绍 Weblog ...
- python异常捕获异常堆栈输出
python异常捕获异常堆栈输出 学习了:https://blog.csdn.net/chris_grass/article/details/77927902 import traceback def ...
- IntelliJ IDEA cannot resolved 处理
IntelliJ IDEA cannot resolved 处理 学习了:https://stackoverflow.com/questions/21577573/intellij-idea-can- ...
- 使用Python写的第一个网络爬虫程序
今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中. 此代码中主要使用到了python的以下几个功能,因为对python不 ...
- v-if v-else-if v-else
1.代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- UFLDL教程笔记及练习答案三(Softmax回归与自我学习***)
:softmax回归 当p(y|x,theta)满足多项式分布,通过GLM对其进行建模就能得到htheta(x)关于theta的函数,将其称为softmax回归. 教程中已经给了cost及gradie ...