Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graph/test/shortest_path_dial.yuki1695.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int solve(const string &S, const string &T) {
if (!nmatch) return INF;
if (T.size() % 2) return INF;
auto trev = T;
reverse(trev.begin(), trev.end());
if (trev != T) return INF;
shortest_path<int> graph(T.size() + 1);
for (int i = 0; i < int(T.size()); ++i) graph.add_edge(i, i + 1, 0);
Expand All @@ -23,7 +24,7 @@ int solve(const string &S, const string &T) {
if ((l + r) % 2 == 0) graph.add_edge(r, (l + r) / 2, 1);
}
graph.dial(T.size(), nmatch);
return graph.dist[nmatch];
return std::max(1, graph.dist[nmatch]);
}

int main() {
Expand Down
3 changes: 2 additions & 1 deletion graph/test/zero_one_bfs.yuki1695.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int solve(const string &S, const string &T) {
if (!nmatch) return INF;
if (T.size() % 2) return INF;
auto trev = T;
reverse(trev.begin(), trev.end());
if (trev != T) return INF;
shortest_path<int> graph(T.size() + 1);
for (int i = 0; i < int(T.size()); ++i) graph.add_edge(i, i + 1, 0);
Expand All @@ -23,7 +24,7 @@ int solve(const string &S, const string &T) {
if ((l + r) % 2 == 0) graph.add_edge(r, (l + r) / 2, 1);
}
graph.zero_one_bfs(T.size(), nmatch);
return graph.dist[nmatch];
return std::max(1, graph.dist[nmatch]);
}

int main() {
Expand Down
7 changes: 5 additions & 2 deletions string/manacher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <utility>
#include <vector>

// CUT begin
// Manacher's Algorithm: radius of palindromes
// Input: std::string or std::vector<T> of length N
// Output: std::vector<int> of size N
Expand All @@ -30,8 +29,13 @@ std::vector<int> manacher(const std::string &S) {
return manacher(v);
}

// Find maximal palindrome length for each center
// input: array of length N
// output: array of length N * 2 - 1
template <typename T>
std::vector<std::pair<int, int>> enumerate_palindromes(const std::vector<T> &vec) {
if (vec.empty()) return {};

std::vector<T> v;
const int N = vec.size();
for (int i = 0; i < N - 1; i++) {
Expand All @@ -52,7 +56,6 @@ std::vector<std::pair<int, int>> enumerate_palindromes(const std::vector<T> &vec
}
return ret;
}

std::vector<std::pair<int, int>> enumerate_palindromes(const std::string &S) {
std::vector<char> v(S.size());
for (int i = 0; i < int(S.size()); i++) v[i] = S[i];
Expand Down
5 changes: 5 additions & 0 deletions string/manacher.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ for (auto [l, r] : palindromes) {
}
```

## 問題例

- [Enumerate Palindromes](https://judge.yosupo.jp/problem/enumerate_palindromes)
- [No.3392 Count 23578 Sequence - yukicoder](https://yukicoder.me/problems/no/3392)

## 文献

- [1] G. Manacher, "A new linear-time "on-line" algorithm for finding the smallest initial palindrome of a string", Journal of the ACM, 22 (3), 346-351, 1975.
22 changes: 22 additions & 0 deletions string/test/manacher.yuki3392.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define PROBLEM "https://yukicoder.me/problems/no/3392"
#include "../manacher.hpp"

#include <iostream>
using namespace std;

int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);

int N;
cin >> N;
vector<int> A(N);
for (auto &a : A) cin >> a;

vector<int> D(N - 1);
for (int i = 0; i < N - 1; ++i) D.at(i) = A.at(i + 1) - A.at(i);

long long ret = N;
for (auto [l, r] : enumerate_palindromes(D)) ret += (r - l + 1) / 2;

cout << ret << '\n';
}