hiho Week 3 register

Ended

Participants:669

Verdict:Accepted
Submitted:2014-07-20 10:35:44

Lang:G++

Edit
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
#include <iostream>
#include <string>
using namespace std;
int kmp(const string &s, const string &p) {
    const int N = s.length();
    const int M = p.length();
    if ( M == 0 ) return 0;
    int skips[M];
    skips[0] = -1;
    int i = 1;
    int j = skips[i - 1];
    while ( i < M ) {
        if ( j < 0 ) {
            skips[i ++] = j = 0;
        } else if ( p[i - 1] == p[j] ) {
            skips[i ++] = ++ j;
        } else {
            j = skips[j];
        }
    }
    
    int count = 0;
    i = j = 0;
    while ( i < N ) {
        if ( j < 0 ) {
            i ++;
            j = 0;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX