-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ32.cc
51 lines (44 loc) · 1003 Bytes
/
SPOJ32.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPOJ 32: A Needle in the Haystack
// http://www.spoj.com/problems/NHAY/
//
// Solution: string matching in stream (KMP)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
using namespace std;
#define ALL(c) c.begin(), c.end()
#define FOR(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define fst first
#define snd second
void solve(int n) {
char p[n];
scanf("%s\n", p);
// KMP table
int f[n+1], j = f[0] = -1;
for (int i = 1; i <= n; ++i) {
while (j >= 0 && p[j] != p[i-1]) j = f[j];
f[i] = ++j;
}
int count = 0;
for (int i = 0, k = 0; ; ++i) {
char c = getchar();
if (c == '\n') break;
while (k >= 0 && p[k] != c) k = f[k];
if (++k >= n) {
printf("%d\n", i-n+1);
++count;
k = f[k];
}
}
if (count == 0) printf("\n");
}
int main() {
for (int n; scanf("%d\n", &n) == 1 ;) {
solve(n);
}
}