mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 19:08:47 +00:00
#1746. 【2022.11.8 联考】回文(palindrome)
https://sjzezoj.com/submission/64047
This commit is contained in:
parent
e689335869
commit
de8d986270
101
S2OJ/1746/1746.cpp
Normal file
101
S2OJ/1746/1746.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 300005;
|
||||
|
||||
int q, st[N << 1][20];
|
||||
std::string s;
|
||||
|
||||
std::vector<int> manacher(const std::string &_s) {
|
||||
int n = _s.size();
|
||||
std::string s(n * 2 + 3, '#');
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
s[i * 2 + 2] = _s[i];
|
||||
}
|
||||
|
||||
s[0] = '^';
|
||||
s[n * 2 + 2] = '$';
|
||||
|
||||
std::vector<int> p(s.size(), 1);
|
||||
|
||||
for (int i = 1, mid = 0, r = 0; i <= n * 2 + 1; i++) {
|
||||
p[i] = i < r ? std::min(p[mid * 2 - i], r - i) : 1;
|
||||
|
||||
while (s[i - p[i]] == s[i + p[i]]) p[i]++;
|
||||
|
||||
if (i + p[i] - 1 > r) {
|
||||
r = i + p[i] - 1;
|
||||
mid = i;
|
||||
}
|
||||
}
|
||||
|
||||
// for (int i = 0; i < n; i++) {
|
||||
// p[i] = p[i * 2 + 2] / 2;
|
||||
// }
|
||||
|
||||
// p.resize(n);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
int query(int l, int r) {
|
||||
if (l > r) return 1;
|
||||
int k = std::__lg(r - l + 1);
|
||||
return std::max(st[l][k], st[r - (1 << k) + 1][k]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> s >> q;
|
||||
|
||||
auto p = manacher(s);
|
||||
int n = s.size();
|
||||
int t = std::__lg(p.size());
|
||||
|
||||
for (int i = 1; i <= p.size(); i++) {
|
||||
st[i][0] = p[i - 1];
|
||||
}
|
||||
|
||||
for (int i = 1; i <= t; i++) {
|
||||
for (int j = 1; j <= p.size() - (1 << i) + 1; j++) {
|
||||
st[j][i] = std::max(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int ql, qr;
|
||||
|
||||
cin >> ql >> qr;
|
||||
|
||||
ql = (ql + 1) * 2 + 1;
|
||||
qr = (qr + 1) * 2 + 1;
|
||||
|
||||
int l = 1,
|
||||
r = qr - ql + 1,
|
||||
res = 1;
|
||||
|
||||
while (l <= r) {
|
||||
int mid = l + r >> 1;
|
||||
|
||||
if (query(ql + mid - 1, qr - mid + 1) - 1 >= mid) {
|
||||
res = mid;
|
||||
l = mid + 1;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
S2OJ/1746/data/ex_palindrome1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/ex_palindrome4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/ex_palindrome4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome10.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome5.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome6.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome6.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome7.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome7.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome8.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome8.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome9.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/palindrome9.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/palindrome9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1746/data/problem.conf
(Stored with Git LFS)
Normal file
BIN
S2OJ/1746/data/problem.conf
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user