이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll base = 21121;
int add (int a, int b) { return a + b - (a + b < MOD ? 0 : MOD); }
int sub (int a, int b) { return a - b + (a - b >= 0 ? 0 : MOD); }
int mul (int a, int b) { return 1LL * a * b % MOD; }
ll change[mn][26], basePow[mn], n;
vector<pii> open[mn], close[mn];
struct BIT {
vector<int> tr;
BIT (int sz) : tr(sz + 1) {}
int p (int k) { return k & -k; }
void update (int k, int delta) {
if (delta < 0) delta += MOD;
for (int t = 0; k < tr.size(); t += p(k), k += p(k))
tr[k] = add(tr[k], mul(delta, basePow[t]));
}
int preSum (int k) {
int ans = 0;
for (int t = 0; k; t += p(k), k -= p(k))
ans = add(ans, mul(tr[k], basePow[t]));
return ans;
}
int query (int l, int r) {
return sub(preSum(r), mul(preSum(l - 1), basePow[r - l + 1]));
}
} preHash(mn), sfxHash(mn);
int rev (int i) { return n - i + 1; }
bool valid (int l, int r) {
if (l < 1 || r > n) return 0;
if (l == r) return 1;
return preHash.query(l, r) == sfxHash.query(rev(r), rev(l));
}
bool checkOdd (int core, int wing) {
return valid(core - wing + 1, core + wing - 1);
}
bool checkEven (int core, int wing) {
return valid(core - wing + 1, core + wing);
}
int calcOdd (int i) {
int wingOdd = 0;
for (int msk = 1 << 15; msk > 0; msk >>= 1)
if (checkOdd(i, wingOdd | msk)) wingOdd |= msk;
return wingOdd;
}
int calcEven (int i) {
int wingEven = 0;
for (int msk = 1 << 15; msk > 0; msk >>= 1)
if (checkEven(i, wingEven | msk)) wingEven |= msk;
return wingEven;
}
void update (int i, int c) {
preHash.update(i, c);
sfxHash.update(rev(i), c);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
basePow[0] = 1;
for (int i = 1; i < mn; i++)
basePow[i] = basePow[i - 1] * base % MOD;
string s; cin >> s;
n = s.length(), s = " " + s;
for (int i = 1; i <= n; i++) update(i, s[i]);
ll curAns = 0;
for (int i = 1; i <= n; i++) {
int wingOdd = calcOdd(i), wingEven = calcEven(i);
curAns += wingOdd + wingEven;
//cout << "dbg " << i << " " << wingOdd << " " << wingEven << "\n";
if (1 <= i - wingOdd && i + wingOdd <= n) {
ll delta = s[i + wingOdd] - s[i - wingOdd];
update(i - wingOdd, delta);
ll contrib = calcOdd(i) - wingOdd;
change[i - wingOdd][s[i + wingOdd] - 'a'] += contrib;
change[i + wingOdd][s[i - wingOdd] - 'a'] += contrib;
update(i - wingOdd, -delta);
}
if (1 <= i - wingEven && i + wingEven + 1 <= n) {
ll delta = s[i + wingEven + 1] - s[i - wingEven];
update(i - wingEven, delta);
ll contrib = calcEven(i) - wingEven;
change[i - wingEven][s[i + wingEven + 1] - 'a'] += contrib;
change[i + wingEven + 1][s[i - wingEven] - 'a'] += contrib;
update(i - wingEven, -delta);
}
if (wingOdd > 1) {
// disturb odd palindrome
open[i - wingOdd + 1].emplace_back(i - wingOdd, 1), close[i - 1].emplace_back(i - wingOdd, 1);
open[i + 1].emplace_back(i + wingOdd, 0), close[i + wingOdd - 1].emplace_back(i + wingOdd, 0);
}
if (wingEven > 0) {
// disturb even palindrome
open[i - wingEven + 1].emplace_back(i - wingEven, 1), close[i].emplace_back(i - wingEven, 1);
open[i + 1].emplace_back(i + wingEven + 1, 0), close[i + wingEven].emplace_back(i + wingEven + 1, 0);
}
}
// try changing each letter
ll ans = curAns, sumLeft = 0, cntLeft = 0, sumRight = 0, cntRight = 0;
for (int i = 1; i <= n; i++) {
for (pii item : open[i]) {
int p, type; tie(p, type) = item;
if (type) sumLeft += p, cntLeft++;
else sumRight += p, cntRight++;
}
ll rem = i * cntLeft - sumLeft + sumRight - i * cntRight;
for (int j = 0; j < 26; j++)
if (j != s[i] - 'a') ans = max(ans, curAns - rem + change[i][j]);
for (pii item : close[i]) {
int p, type; tie(p, type) = item;
if (type) sumLeft -= p, cntLeft--;
else sumRight -= p, cntRight--;
}
}
cout << ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
palinilap.cpp: In member function 'void BIT::update(int, int)':
palinilap.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int t = 0; k < tr.size(); t += p(k), k += p(k))
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |