ICPC Notebook

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub tatyam-prime/ICPC_notebook

:heavy_check_mark: test/string/Manacher.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_palindromes"
#include "test/template.hpp"
#include "src/string/Manacher.hpp"

int main() {
   cin.tie(0)->sync_with_stdio(0);
   string S;
   cin >> S;
   const ll N = sz(S);
   string T(N * 2 + 1, '$');
   rep(i, 0, N) T[i * 2 + 1] = S[i];
   auto r = manacher(T);
   rep(i, 1, N * 2) {
      if(i & 1) cout << r[i] - 1 << " \n"[i + 1 == N * 2];
      else cout << r[i] - 1 << ' ';
   }
}
#line 1 "test/string/Manacher.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_palindromes"
#line 1 "test/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LLONG_MAX / 4;
#define rep(i, a, b) for(ll i = a; i < (b); i++)
#define all(a) begin(a), end(a)
#define sz(a) ssize(a)
bool chmin(auto& a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, auto b) { return a < b ? a = b, 1 : 0; }
#line 1 "src/string/Manacher.hpp"
// 各位置での回文半径を求める
// aaabaaa -> 1214121
// 偶数長の回文を含めて直径を知るには,N+1 個の $ を挿入して 1 を引く
// $a$a$a$b$a$a$a$ -> 123432181234321
auto manacher(string s) {
   ll n = sz(s), i = 0, j = 0;
   vector<ll> r(n);
   while(i < n) {
      while(i >= j && i + j < n && s[i - j] == s[i + j]) j++;
      r[i] = j;
      ll k = 1;
      while(i >= k && i + k < n && k + r[i - k] < j) {
         r[i + k] = r[i - k];
         k++;
      }
      i += k, j -= k;
   }
   return r;
}
#line 4 "test/string/Manacher.test.cpp"

int main() {
   cin.tie(0)->sync_with_stdio(0);
   string S;
   cin >> S;
   const ll N = sz(S);
   string T(N * 2 + 1, '$');
   rep(i, 0, N) T[i * 2 + 1] = S[i];
   auto r = manacher(T);
   rep(i, 1, N * 2) {
      if(i & 1) cout << r[i] - 1 << " \n"[i + 1 == N * 2];
      else cout << r[i] - 1 << ' ';
   }
}
Back to top page