-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZPattern.h
More file actions
107 lines (89 loc) · 3.73 KB
/
ZPattern.h
File metadata and controls
107 lines (89 loc) · 3.73 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Author: Moisés Adame-Aguilar
// Date: September 26th, 2023
// Description: Implementation of the Z-Function String Pattern Algorithm.
#ifndef __ZPattern
#define __ZPattern
#pragma once
#include<vector>
using namespace std;
class ZPattern {
public:
string mainString;
string substring;
vector<int> indexes;
// Constructor for the Z-Function String Pattern Algorithm.
ZPattern(string mainString, string substring){
this->mainString = mainString;
this->substring = substring;
this->combinationString = substring + "$" + mainString;
}
// Function that prints the mainString, substring and the combinationString
void print(){
cout << this->mainString << endl;
cout << this->substring << endl;
cout << this->combinationString << endl;
}
// Function that returns a bool that represents the existance of the substring
// inside the mainString using the Z-Function Algorithm.
// Complexity: O(m + n)
bool compare(){
int combinationStringSize = combinationString.size();
int substringSize = substring.size();
vector<int> zfunctionArray = buildZFunction();
// Check if in the zfunctionArray there is a value equal to the substringSize.
for(int i = substringSize + 1; i < combinationStringSize; i++){
if(zfunctionArray[i] == substringSize){
indexes.push_back(i - substringSize - 1);
}
}
// If the indexes vector is empty, there was no match.
return !indexes.empty();
}
int getIndexLongestSimilarity(){
int combinationStringSize = combinationString.size();
int substringSize = substring.size();
int indexLongestSimilarity = 0;
int longestSimilaritySize = 0;
int valueArray = 0;
vector<int> zfunctionArray = buildZFunction();
for(int i = substringSize + 1; i < combinationStringSize; i++){
valueArray = zfunctionArray[i];
if(valueArray > longestSimilaritySize){
longestSimilaritySize = valueArray;
indexLongestSimilarity = i - substringSize - 1;
}
}
return indexLongestSimilarity;
}
private:
string combinationString;
// Function that builds the zfunctionArray of the combinationString.
// Complexity: O(n + m)
vector<int> buildZFunction(){
// Generate the zfunctionArray with it's respective values.
int combinationStringSize = combinationString.size();
int substringSize = substring.size();
vector<int> zfunctionArray;
zfunctionArray.resize(combinationStringSize);
fill(zfunctionArray.begin(), zfunctionArray.end(), 0);
int left = 0, right = 0;
for(int i = 1; i < combinationStringSize; i++){
if(i < right){
zfunctionArray[i] = min(right - i, zfunctionArray[i - left]);
}
while(i + zfunctionArray[i] < combinationStringSize && combinationString[i + zfunctionArray[i]] == combinationString[zfunctionArray[i]]){
zfunctionArray[i]++;
}
if(i + zfunctionArray[i] > right){
left = i;
right = i + zfunctionArray[i];
}
}
return zfunctionArray;
}
// Function that returns the max between min integers.
int min(int num1, int num2){
return num1 <= num2 ? num1 : num2;
}
};
#endif