Skip to content

Commit

Permalink
Added manacher algo
Browse files Browse the repository at this point in the history
  • Loading branch information
dothething1DA committed Nov 22, 2024
1 parent 493c384 commit 40e28be
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 47 deletions.
1 change: 1 addition & 0 deletions content/kactl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
\kactlchapter{contest}
\kactlchapter{data-structures}
\kactlchapter{graph}
\kactlchapter{string}
\kactlchapter{number-theory}
\kactlchapter{geometry}
\kactlchapter{math}
Expand Down
3 changes: 3 additions & 0 deletions content/string/chapter.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\chapter{String}

\kactlimport{manacher.cpp}
52 changes: 52 additions & 0 deletions content/string/manacher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Author: me
* Description: Find longest palindrome centered at each index.
* Initialise with original 0-indexed string. QUERYING IS 1-INDEXED.
* Time: $O(N)$
*/

struct Manacher {
string s;
vector<int> pl;

void calc() {
int n = s.length() - 1;
int l = 0, r = 0;

for (int i = 1; i <= n; i += 1) {
if (i < r) pl[i] = min(r-i, pl[l+r-i]);
while (s[i-pl[i]-1] == s[i+pl[i]+1]) pl[i] += 1;
if (i + pl[i] > r) {
l = i - pl[i];
r = i + pl[i];
}
}
}

Manacher() {}
void init(string _s, char f='~') {
s = "@"; s += f;
for (char c: _s)
{s += c; s += f;} s += '|';
pl.resize(s.length(), 0);
calc();
}

// Query for substring [l, r]
// on original string with 1-indexed
bool is_palindrome(int l, int r) {
l <<= 1; r <<= 1;
int mid = (l+r) >> 1;
return mid + pl[mid] >= r;
}
};
/*
Manacher mana;
void example() {
string s;
cin >> s;
mana.init(s);
int l, r; cin >> l >> r;
cout << mana.is_palindrome(l, r);
}*/
Binary file modified kactl.pdf
Binary file not shown.
Binary file added lab/code
Binary file not shown.
59 changes: 59 additions & 0 deletions lab/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Author: me
* Description: Find longest palindrome centered at each index.
* Initialise with original 0-indexed string. QUERYING IS 1-INDEXED.
* Time: $O(N)$
*/

#include <bits/stdc++.h>
using namespace std;

struct Manacher {
string s;
vector<int> pl;

void calc() {
int n = s.length() - 1;
int l = 0, r = 0;

for (int i = 1; i <= n; i += 1) {
if (i < r) pl[i] = min(r-i, pl[l+r-i]);
while (s[i-pl[i]-1] == s[i+pl[i]+1]) pl[i] += 1;
if (i + pl[i] > r) {
l = i - pl[i];
r = i + pl[i];
}
}
}

Manacher() {}
void init(string _s, char f='~') {
s = "@"; s += f;
for (char c: _s)
{s += c; s += f;} s += '|';
pl.resize(s.length(), 0);
calc();
}

// Query for substring [l, r]
// on original string with 1-indexed
bool is_palindrome(int l, int r) {
l <<= 1; r <<= 1;
int mid = (l+r) >> 1;
return mid + pl[mid] >= r;
}
};

Manacher mana;

void example() {
string s;
cin >> s;
mana.init(s);
int l, r; cin >> l >> r;
cout << mana.is_palindrome(l, r);
}

int main() {
example();
}
47 changes: 0 additions & 47 deletions lab/sparse_table.cpp

This file was deleted.

0 comments on commit 40e28be

Please sign in to comment.