diff --git a/.gitignore b/.gitignore index a1570c3..2b7c7ea 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Lab3/main.exe Lab4/main Lab4/main.exe .DS_Store +Lab4/a.exe diff --git a/Lab4/.vscode/settings.json b/Lab4/.vscode/settings.json new file mode 100644 index 0000000..a3b2b51 --- /dev/null +++ b/Lab4/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "vector": "cpp" + } +} \ No newline at end of file diff --git a/Lab4/main.cpp b/Lab4/main.cpp index 825f0f3..29f3a97 100644 --- a/Lab4/main.cpp +++ b/Lab4/main.cpp @@ -42,11 +42,11 @@ int main(void){ // This function computes the longest prefix suffix array for the pattern int* computeLPS(string pattern){ - int m = pattern.length(); - int *lps = new int[m]; + int pattern_lenght = pattern.length(); + int *lps = new int[pattern_lenght]; lps[0] = 0; int i = 1; int j = 0; // i is the index of the suffix and j is the index of the prefix - while(i < m){ // we start from the second character of the pattern + while(i < pattern_lenght){ if(pattern[i] == pattern[j]){ // if the characters match, we increment both i and j j++; lps[i] = j; // the value of lps[i] is the length of the longest prefix that is also a suffix of the substring pattern[0..i] @@ -62,6 +62,7 @@ int* computeLPS(string pattern){ } } } + return lps; } @@ -69,19 +70,19 @@ int* computeLPS(string pattern){ // This function returns the positions of the occurences of the pattern in the text vector kmp(string text, string pattern){ vector positions; - int n = text.length(); - int m = pattern.length(); + int text_lenght = text.length(); + int pattern_lenght = pattern.length(); int i = 0; int j = 0; // i is the index of text and j is the index of pattern int *lps = computeLPS(pattern); - while(i < n){ + while(i < text_lenght){ if(text[i] == pattern[j]){ // if the characters match, increment both i and j i++; j++; } - if(j == m){ // if j reaches the end of the pattern, we have found a match + if(j == pattern_lenght){ // if j reaches the end of the pattern, we have found a match positions.push_back(i-j); j = lps[j-1]; } - else if(i < n && text[i] != pattern[j]){ // if the characters don't match, we need to move j back to the last matching character + else if(i < text_lenght && text[i] != pattern[j]){ // if the characters don't match, we need to move j back to the last matching character if(j != 0){ // if j is not at the beginning of the pattern, we move it back to the last matching character j = lps[j-1]; }