Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
NotSoNaive.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/measure>
9
10#include <alib/set>
11#include <alib/vector>
12
13#include <string/LinearString.h>
14
15
16namespace stringology {
17
18 namespace exact {
19
20
25 public:
30 template < class SymbolType >
32
33 };
34
35 template < class SymbolType >
38
40 // if pattern is one char, skip the rest and use naive approach
41 if (pattern.getContent().size() == 1) {
42 for (unsigned i = 0; i < subject.getContent().size(); ++i) {
43 if (subject.getContent()[i] == pattern.getContent()[0]) occ.insert(i);
44 }
45 return occ;
46 }
47
48 if (pattern.getContent()[0] != pattern.getContent()[1]) {
49 for (unsigned i = 0; i + pattern.getContent().size() <= subject.getContent().size(); ++i) {
50 bool match = true ;
51 // try to match the whole string, in case of mathing fist two characters you can schift by 2
52 for (unsigned j = 0; j < pattern.getContent().size(); ++j) {
53 if (subject.getContent()[i + j] != pattern.getContent()[j]) {
54 match = false ;
55 if (j > 1) {
56 ++i;
57 break;
58 } else break;
59 }
60 }
61 if ( match ) occ.insert(i);
62 }
63 } else {
64 for (unsigned i = 0; i + pattern.getContent().size() <= subject.getContent().size(); ++i) {
65 bool match = true ;
66 // try to match the whole string, if pattern[0] matches and pattern[1] not, then advance by 2
67 for (unsigned j = 0; j < pattern.getContent().size(); ++j) {
68 if (subject.getContent()[i + j] != pattern.getContent()[j]) {
69 match = false ;
70 if (j == 1) {
71 ++i;
72 break;
73 } else break;
74 }
75 }
76 if ( match ) occ.insert(i);
77 }
78 }
80 return occ;
81
82 }
83
84 } /* namespace exact */
85
86} /* namespace stringology */
87
Definition: set.hpp:44
Linear string.
Definition: LinearString.h:57
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
Definition: NotSoNaive.h:24
static ext::set< unsigned > match(const string::LinearString< SymbolType > &subject, const string::LinearString< SymbolType > &pattern)
Definition: NotSoNaive.h:36
int i
Definition: AllEpsilonClosure.h:118
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14
void end()
Definition: measurements.cpp:19
Definition: ArithmeticCompression.h:18