Skip to content

Commit

Permalink
Create manacher.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Jul 29, 2024
1 parent c46b498 commit 51b5e10
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ps/string/manacher/manacher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//ref: https://www.crocus.co.kr/1075
void manachers(string st, int n){
int r = 0, p = 0;
for (int i = 0; i < n; i++){
arr[i] = (i <= r) ? min(arr[2 * p - i], r - i) : 0;
while (i - arr[i] - 1 >= 0 && i + arr[i] + 1 < n && st[i - arr[i] - 1] == st[i + arr[i] + 1]) ++arr[i];
if (r < i + arr[i]) r = i + arr[i], p = i;
}
}

0 comments on commit 51b5e10

Please sign in to comment.