A - Number Sequence HDU-1711

题意:

反正就是给出一个T,然后每个T:一个n,一个m,表示主串的数字数量和模式串的数字数量,求第一个匹配的位置。

思路:

KMP模板套套就好啦。

参考代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <map>
 4 #include <string>
 5 #include <queue>
 6 #include <stack>
 7 #include <set>
 8 #include <algorithm>
 9
10 #include <cstdio>
11 #include <cstring>
12 #include <cmath>
13 #include <cstdlib>
14 using namespace std;
15
16 const int INF=0x3f3f3f3f;
17 const int SIZE=10000;
18 typedef long long LL;
19
20 int nextt[10005];
21 int a[1000005];
22 int b[10005];
23 int n,m;
24 void findnext()
25 {
26     memset(nextt,0,sizeof(nextt));
27     int len=m;
28     nextt[0]=-1;
29     int k=-1,j=0;
30     while(j<len)
31     {
32         if(k==-1||b[k]==b[j])
33         {
34             k++;
35             j++;
36             if(b[j]!=b[k])
37                 nextt[j]=k;
38             else
39                 nextt[j]=nextt[k];
40         }
41         else
42         {
43             k=nextt[k];
44         }
45     }
46 }
47
48 int kmp()
49 {
50     int i=0,j=0;
51     int sa=n,sb=m;
52     while(i<sa&&j<sb)
53     {
54         if(j==-1||a[i]==b[j])
55         {
56             i++;
57             j++;
58         }
59         else
60         {
61             j=nextt[j];
62         }
63     }
64     if(j==sb)
65         return i-j+1;
66     else
67         return -1;
68 }
69 int main()
70 {
71     int t;
72     scanf("%d",&t);
73     while(t--)
74     {
75         scanf("%d%d",&n,&m);
76         for(int i=0;i<n;i++)
77         {
78             scanf("%d",&a[i]);
79         }
80         for(int j=0;j<m;j++)
81             scanf("%d",&b[j]);
82         findnext();
83         int res=kmp();
84         printf("%d\n",res);
85
86     }
87     return 0;
88 }

时间: 2024-10-25 01:53:20

A - Number Sequence HDU-1711的相关文章

(模板题)Number Sequence -- Hdu -- 1711

http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16080    Accepted Submission(s): 7100 Problem Description Given two sequences of n

Number Sequence HDU - 1711

Given two sequences of numbers : a11 , a22 , ...... , aNN , and b11 , b22 , ...... , bMM (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make aKK = b11 , aK+1K+1 = b22 , ...... , aK+M?1K+M?1 = bMM . If there are mor

Number Sequence HDU 1711 KMP 模板

题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<m

Number Sequence - HDU 1711(KMP模板题)

题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ============================================================================================== #include<stdio.h> #include<string.h> const int MAXM = 1e4+7; const int

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

hdu 1711 Number Sequence(KMP)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int n,m,next[10010],a[1000010],b[10010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<m) { if(j==-1||b[i]==b[j]) i++,j++,next[i]=j; else j=next[

HDU 1711 Number Sequence KMP题解

KMP查找整数数列,不是查找字符串. 原理是一样的,不过把字符串转换为数列,其他基本上是一样的. #include <stdio.h> #include <string.h> const int MAX_N = 1000001; const int MAX_M = 10001; int strN[MAX_N], strM[MAX_M], next[MAX_M], N, M; void getNext() { memset(next, 0, sizeof(int) * M); for

HDU - 1711 Number Sequence KMP字符串匹配

HDU - 1711 Number Sequence Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <=

HDU 1711 Number Sequence(字符串匹配)

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10571    Accepted Submission(s): 4814 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],