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]能够描写叙述为"不为自身的最大首尾反复子串 ...
随机推荐
- R语言入门视频笔记--4--R的数据输入
输入 R的数据输入可以大体三种: 1.键盘输出 2.从文本文件导入 3.从Excel中导入数据 一.从键盘输入 首先创建一个数据框,玩玩嘛,瞎建一个 mydata <- data.frame(a ...
- android图片上传
package com.example.center; import java.io.ByteArrayOutputStream;import java.io.InputStream; import ...
- LeetCode OJ--Valid Palindrome
http://oj.leetcode.com/problems/valid-palindrome/ 判断是否为回文串 bool isPalindrome(string s) { ,j = s.leng ...
- 虚拟机centos 里tomcat的端口映射到主机 Windows里面
- Mac OS X 下安装python的MySQLdb模块
参考资料: mac os x下python安装MySQLdb模块 http://www.codeif.com/post/1073/ MAC OSX使用Python安装模块有关问题 http:// ...
- SPOJ 26108 TRENDGCD - Trending GCD
Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...
- cef network-settings
Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...
- android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)
http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...
- jquery+css实现邮箱自动补全
今天在公司做一个电子商务网站的注册会员时,要求用户在电子邮箱文本框中输入时,给与热点提示常用的电子邮箱,帮助用户选择,提高体验效果.下面是用Jquery+css实现的邮箱自动补全,供大家参考和学习. ...
- jmeter.properties控制聚合报告的用户响应时间设置和smmary results
jmeter.properties的配置Summariser控制输出Summary Results,可以显式rt和tps等信息 Aggregate Report配置可以控制聚合报告的内容,控制90%用 ...