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的更多相关文章

  1. 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 ...

  2. Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution

    class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...

  3. TSearch & TFileSearch Version 2.2 -Boyer-Moore-Horspool search algorithm

    unit Searches; (*-----------------------------------------------------------------------------* | Co ...

  4. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  5. [Algorithm] A* Search Algorithm Basic

    A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...

  6. [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 ...

  7. [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 ...

  8. 笔试算法题(48):简介 - A*搜索算法(A Star Search Algorithm)

    A*搜索算法(A Star Search Algorithm) A*算法主要用于在二维平面上寻找两个点之间的最短路径.在从起始点到目标点的过程中有很多个状态空间,DFS和BFS没有任何启发策略所以穷举 ...

  9. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

随机推荐

  1. 如何修改MFC的图标

    原文:如何修改MFC的图标 修改左上角的图标和任务栏里图标 在对话框构造函数中 CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestD ...

  2. 流行-Manifold【0】-维基百科中文版本解释

  3. python 深复制和浅复制

    https://www.python-course.eu/python3_deep_copy.php-------------------------------------------------- ...

  4. sizeof 感知重载,模板具现, 转换规则

    问题:如何侦知任意型别 T 是否可以自动转换为型别 U? 方案:侦测转换能力的想法:合并运用 sizeof 和重载函数. 1 依赖 sizeof,sizeof 有着惊人的能力,你可以把 sizeof  ...

  5. vuex相关(actions和mutation的异曲同工)

    vuex说明: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 包含的内容: state: ...

  6. 树莓派 -- 按键 (key)使用BCM2835 gpio library

    BCM2835 GPIO library介绍 This is a C library for Raspberry Pi (RPi). It provides access to GPIO and ot ...

  7. JS 比较运算符 逻辑运算符

    逻辑运算符 三元运算符 摘自:http://www.w3school.com.cn/js/js_comparisons.asp

  8. LeetCode(41)First Missing Positive

    题目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...

  9. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  10. 使用idea搭建ssh项目

    参考: https://www.cnblogs.com/getchen/p/8036709.html 需要自己提前在数据库中建好表 然后连接数据库通过侧边栏的persistence来生成实体类和相应的 ...