-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSunday.h
35 lines (31 loc) · 854 Bytes
/
Sunday.h
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
#ifndef SUNDAY_H
#define SUNDAY_H
#include <string.h>
class SundayFinder
{
public:
SundayFinder(const char *pattern):
m_pattern(pattern)
{
for (int i = 0; i < 256; ++i) m_jumpTable[i] = (int)m_pattern.size() + 1;
for (int i = 0; i < (int)m_pattern.size(); ++i) {
m_jumpTable[(int)m_pattern[i]] = (int)m_pattern.size() - i;
}
}
int find(const char *src)
{
const char *pattern = m_pattern.c_str();
const char *end = src + strlen(src) - m_pattern.size() + 1;
int patternSize = (int)m_pattern.size();
int r = 0;
while (src < end) {
if (strncmp(src, pattern, patternSize) == 0) ++r;
src += m_jumpTable[(int)src[patternSize]];
}
return r;
}
private:
string m_pattern;
int m_jumpTable[256];
};
#endif