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]能够描写叙述为"不为自身的最大首尾反复子串 ...
随机推荐
- 1861 奶牛的数字游戏 2006年USACO
codevs——1861 奶牛的数字游戏 2006年USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 题目描述 Descript ...
- python3.x对python2.x变动
原文地址:http://rookiedong.iteye.com/blog/1185403 python 2.4 与 python 3.0 的比较 一. print 从语句变为函数 原: pr ...
- Maven自动部署(SCM-SVN/Git)(maven-scm-plugin/maven-release-plugin插件的使用)
以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_deployment_automation.html ...
- crontab 实际的应用
每二天执行一次: 0 0 */2 * * command #注意分,时不能为星*,否则每分钟执行 每天零晨01,03执行: 0 01,03 * * * command 每2小时执行一次 0 */2 * ...
- MySQL 5.6.20-4 and Oracle Linux DTrace
https://blogs.oracle.com/wim/entry/mysql_5_6_20_4?utm_source=tuicool&utm_medium=referral By WimC ...
- openfalcon的安装和使用
蛮复杂的样子 根据官方文档指导,一步一步走起:https://book.open-falcon.org/zh_0_2/quick_install/prepare.html 单机安装的过程:单击安装会把 ...
- 彻底理解javascript 中的事件对象的pageY, clientY, screenY的区别和联系。
说到底, pageY, clientY, screenY的计算,就是要找到参考点, 它们的值就是: 鼠标点击的点----------- 和参考点指点----------的直角坐标系的距离 stacko ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...
- Effective C++ 条款六 若不想使用编译器自动生成的函数,就该明确拒绝
class HomeForSale //防止别人拷贝方法一:将相应的成员函数声明为private并且不予实现 { public: private: HomeForSale(const HomeForS ...
- net spy memcached 使用demo
package memcached; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.mem ...