Rabin_Karp(hash) HDOJ 1711 Number Sequence

题目传送门

 1 /*
 2   Rabin_Karp:虽说用KMP更好,但是RK算法好理解。简单说一下RK算法的原理:首先把模式串的哈希值算出来,
 3         在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个模式串的长度的哈希值向右移动一位
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-5 14:04:26
 8 * File Name     :HDOJ_1711.cpp
 9  ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef unsigned long long ull;
33 typedef long long ll;
34 const int MAXN = 1e6 + 10;
35 const int MAXM = 1e4 + 10;
36 const int INF = 0x3f3f3f3f;
37 const ull KEY = 100000007;
38 int a[MAXN], b[MAXM];
39 int n, m;
40
41 int match(void) {
42     ull h = 1;
43     for (int i=0; i<m; ++i)    h *= KEY;
44     ull ah = 0, bh = 0;
45     for (int i=0; i<m; ++i)    ah = ah * KEY + a[i];
46     for (int i=0; i<m; ++i)    bh = bh * KEY + b[i];
47     for (int i=0; i+m<=n; ++i)    {
48         if (ah == bh)   return i + 1;
49         if (i + m < n) {
50             ah = ah * KEY + a[i+m] - a[i] * h;
51         }
52     }
53     return -1;
54 }
55
56 int main(void)    {     //HDOJ 1711 Number Sequence
57     int T;  scanf ("%d", &T);
58     while (T--) {
59         scanf ("%d%d", &n, &m);
60         for (int i=0; i<n; ++i)    scanf ("%d", &a[i]);
61         for (int i=0; i<m; ++i)    scanf ("%d", &b[i]);
62         printf ("%d\n", match ());
63     }
64
65     return 0;
66 }

 1 /*
 2     题目链接:http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4194
 3     给你两个字符串A,B,请输出B字符串在A字符串中出现了几次。
 4 */
 5 /************************************************
 6  * Author        :Running_Time
 7  * Created Time  :2015-8-5 14:04:26
 8  * File Name     :ZSTU 4194 字符串匹配
 9  ************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef unsigned long long ull;
33 typedef long long ll;
34 const int MAXN = 1e6 + 10;
35 const int INF = 0x3f3f3f3f;
36 const ull KEY = 100000007;
37 char t[MAXN], p[MAXN];
38
39 int Rabin_Karp(void) {
40     int lent = strlen (t);
41     int lenp = strlen (p);
42     ull h = 1;
43     for (int i=0; i<lenp; ++i)    h *= KEY;
44     ull th = 0, ph = 0; int ret = 0;
45     for (int i=0; i<lenp; ++i)    th = th * KEY + t[i];
46     for (int i=0; i<lenp; ++i)    ph = ph * KEY + p[i];
47     for (int i=0; i+lenp<=lent; ++i)    {
48         if (th == ph)   {
49             ret++;
50             for (int j=i; j<=i+lenp-1; ++j) {       //找到了一个模式串,不能再用,整个跳过去
51                 th = th * KEY + t[j+lenp] - t[j] * h;
52             }
53             i += lenp - 1;  continue;
54         }
55         if (i + lenp < lent) {
56             th = th * KEY + t[i+lenp] - t[i] * h;
57         }
58     }
59     return ret;
60 }
61
62 int main(void)    {     //ZSTU 4194 字符串匹配
63     while (scanf ("%s %s", t, p) == 2)  {
64         printf ("%d\n", Rabin_Karp ());
65     }
66
67     return 0;
68 }

Rabin_Karp应用

时间: 2024-11-09 06:13:14

Rabin_Karp(hash) HDOJ 1711 Number Sequence的相关文章

HDOJ ---1711 Number Sequence

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

hdoj 1711 Number Sequence 【KMP】

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

hdoj 1711 Number Sequence【求字串在母串中第一次出现的位置】

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

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(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

杭电 1711 Number Sequence

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