hdu, KMP algorithm, linear string search algorithm, a nice reference provided
reference:
Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.topcoder.com/community/data-science/data-science-tutorials/introduction-to-string-searching-algorithms/
// to be improved
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1000005
#define MAXM 10005
int nums[MAXN];
int cnums[MAXM];
int ffunc[MAXM]={0};
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T,n,m,i,j,k, res;
int *p,*pend, *q, *r, *rend;
if(scanf("%d",&T)!=1) return -1;
while(T-->0 && scanf("%d%d",&n,&m)==2 && n>=m && m>0) {
for(i=1;i<=n;++i) scanf("%d",&nums[i]);
for(i=1;i<=m;++i) scanf("%d",&cnums[i]);
for(i=2;i<=m;++i) {
for(j=ffunc[i-1];;j=ffunc[j]) {
if(cnums[j+1]==cnums[i] || j==0 && --j) break;
}
ffunc[i]=j+1;
}
for(i=1, k=0;;) {
if(nums[i]==cnums[k+1]) { ++k; }
else if(k>0) { k=ffunc[k]; continue; }
if(++i>n || k>=m) break;
}
res=k!=m?-1:i-m;
printf("%d\n",res);
}
return 0;
}
hdu, KMP algorithm, linear string search algorithm, a nice reference provided的更多相关文章
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- TSearch & TFileSearch Version 2.2 -Boyer-Moore-Horspool search algorithm
unit Searches; (*-----------------------------------------------------------------------------* | Co ...
- Aho - Corasick string matching algorithm
Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...
- [Algorithm] A* Search Algorithm Basic
A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...
- [Algorithm] Breadth First JavaScript Search Algorithm for Graphs
Breadth first search is a graph search algorithm that starts at one node and visits neighboring node ...
- 笔试算法题(48):简介 - A*搜索算法(A Star Search Algorithm)
A*搜索算法(A Star Search Algorithm) A*算法主要用于在二维平面上寻找两个点之间的最短路径.在从起始点到目标点的过程中有很多个状态空间,DFS和BFS没有任何启发策略所以穷举 ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
随机推荐
- Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符。而字符集意外的连字符不具有特殊意义。
Perl字符集就是方括号(或称中括号)里一连串可能的字符,只匹配单一字符,该单一字符可以是字符集里的任何一个,“-”在字符集里有特殊含义:表示某个范围的字符.而字符集意外的连字符不具有特殊意义.
- 第2节 mapreduce深入学习:11、maptask运行机制(多看几遍)
mapTask运行机制详解以及mapTask的并行度在mapTask当中,一个文件的切片大小使用默认值是128M,就是跟我们一个block块对应大小一样 MapTask运行的整个过程 背下来1.Tex ...
- NET SignaiR 实现消息的推送,并使用Push.js实现通知
一.使用背景 1. SignalR是什么? ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程.实时 Web 功能是指 ...
- vue mixins应用场景
学习知识得在应用场景中去应用,这样才能真正学到东西,记忆也深刻,以后碰到类似的东西就会了. 1.在assets文件夹下创建一个js文件 // 创建一个需要混入的对象 export const mixi ...
- HYSBZ - 2763 飞行路线(分层图最短路线)
题目: Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价 ...
- P5304 [GXOI/GZOI2019]旅行者(最短路/乱搞)
luogu bzoj Orz自己想出神仙正解的sxy 描述略 直接把所有起点推进去跑dijkstra... 并且染色,就是记录到这个点的最短路是由哪个起点引导出来的 然后再把所有边反指跑一次... 之 ...
- Mvc Action可以通过jsonp方式调取
jsonp其实是一种特殊的数据获取格式,所以在Aicton直接调取的时候肯定会出现问题,下面代码是对于jsonp调取做的处理 protected virtual ActionResult Create ...
- Git和SVN共存的方法
刚工作的时候都是用的cvs和svn,对git不熟悉,随着工作的需要,打分支和版本管理的需要,熟悉起来了git,这一用不可收拾,比svn远远好用,尤其是版本分支管理上,切换分支的方便性,现在这家公司还是 ...
- 在ubuntu 18.04下装有线守护wg
wg你懂的是什么,下面讲讲我这段时间安装过程,发觉这个软件和CENTOS 7下面不是很合拍,它更适合ubuntu,因为ubuntu仍然用iptables,而CENTOS 早就用了firewalld了, ...
- [bzoj1500][NOI2005]维修数列[Treap][可持久化Treap]
非旋转式Treap1500 :) #include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; const i ...