↓ 跳过正文
  1. Posts/

hdu 1711 Number Sequence (kmp)

·356 字·1 分钟

hdu 1711 题目链接

题意:给定两个数列,问第二个数列在第一个数列中出现的位置(第一个元素对应的位置)

思路:数列也可以看成字符串,然后左kmp,返回的答案是i+1-m。。。1A

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月11日 星期四 02时30分23秒
 4File Name :code/hdu/1711.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <stack>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <deque>
19#include <ctime>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E6+7;
34const int M=1E4+7;
35int a[N];
36int b[M];
37int nxt[M];
38int n,m;
39void getnxt(int n)
40{
41    int i = 0;
42    int j = -1;
43    nxt[0] = -1;
44    while (i<n)
45	if (j==-1||b[i]==b[j]) nxt[++i]=++j;
46	else j = nxt[j];
47}
48int kmp( int n,int m)
49{
50    getnxt(m);
51    int i = 0;
52    int j = 0;
53    while (i<n)
54    {
55	if (j==-1||a[i]==b[j]) i++,j++;
56	else j = nxt[j];
57	if (j==m) return i+1-m;
58    }
59    return -1;
60}
61int main()
62{
63	#ifndef  ONLINE_JUDGE
64	freopen("code/in.txt","r",stdin);
65  #endif
66	int T;
67	scanf("%d",&T);
68	while (T--)
69	{
70	    scanf("%d%d",&n,&m);
71	    for ( int i = 0 ; i < n ; i++) scanf("%d",&a[i]);
72	    for ( int i = 0 ; i < m ; i++) scanf("%d",&b[i]);
73	    int ans = kmp(n,m);
74	    printf("%d\n",ans);
75	}
76  #ifndef ONLINE_JUDGE
77  fclose(stdin);
78  #endif
79    return 0;
80}

相关文章

KMP与最小覆盖子串

·1041 字·3 分钟
参考资料(本文大部分是参考这篇博客,附带一些证明步骤的解释) 首先明确一些概念: 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串。 比如: 对于s=“abcab”,它的最小覆盖子串p=“abc”,因为p通过在它后面再接上一个p(即重叠0个字符),可以得到q=“abcabc”,此时s是q的子串。 对于s=“ababab”,它的最小覆盖子串为p=“ab”。

poj 2752 Seek the Name, Seek the Fame (kmp 理解nxt函数)

·298 字·1 分钟
poj 2752题目链接 题意:求出所有的前缀和后缀相同的子串的长度。 思路:求出nxt函数,观察发现,从从len递归向前就是答案。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月10日 星期三 21时05分52秒 4File Name :code/poj/2752.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <deque> 20#include <ctime> 21#define fst first 22#define sec second 23#define lson l,m,rt<<1 24#define rson m+1,r,rt<<1|1 25#define ms(a,x) memset(a,x,sizeof(a)) 26typedef long long LL; 27#define pi pair < int ,int > 28#define MP make_pair 29 30using namespace std; 31const double eps = 1E-8; 32const int dx4[4]={1,0,0,-1}; 33const int dy4[4]={0,-1,1,0}; 34const int inf = 0x3f3f3f3f; 35const int N=4E6+7; 36int n; 37string a; 38int nxt[N]; 39void getnxt( int n) 40{ 41 int i = 0 ; 42 int j = -1 ; 43 nxt[0] = -1; 44 while (i<n) 45 if (j==-1||a[i]==a[j]) nxt[++i]=++j; 46 else j = nxt[j]; 47} 48void print( int x) 49{ 50 if (nxt[x]!=-1) 51 { 52 print(nxt[x]); 53 printf("%d ",x); 54 } 55} 56int main() 57{ 58 #ifndef ONLINE_JUDGE 59 freopen("code/in.txt","r",stdin); 60 #endif 61// ios::sync_with_stdio(false); 62 while (cin>>a) 63 { 64 int len = a.length(); 65 getnxt(len); 66 // for ( int i = 0 ; i < len ; i++) cout<<i<<" nxt[i]:"<<nxt[i]<<endl; 67 print(nxt[len]); 68 printf("%d\n",len); 69 70 } 71 72 #ifndef ONLINE_JUDGE 73 fclose(stdin); 74 #endif 75 return 0; 76}

hdu 1686 Oulipo (kmp模板题)

·473 字·1 分钟
hdu1686 题意:给出模式串和文本串,问模式串在文本串中出现了多少次,可以overlap. 思路:思考naive的匹配过程。nxt函数不过是改进了当失配发生时,不是移动1位,而是移动多位。nxt函数的含义是当失配发生时,移动到的位置….所以有的教程管这个叫失配函数吧,也不是很难理解的样子。