QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#631272 | #7008. Rikka with Nice Counting Striking Back | pskkk | AC ✓ | 6061ms | 91296kb | C++23 | 4.7kb | 2024-10-11 23:33:52 | 2024-10-11 23:33:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
char S[400005];
int n;
namespace sam {
int ch[400005][26], lk[400005], len[400005], nd = 1, las = 1;
void init() {
for (int i = 0; i <= nd; i++) {
memset(ch[i], 0, 104);
lk[i] = len[i] = 0;
}
nd = las = 1;
}
void extend(int c) {
int x = ++nd, p = las;
las = x;
len[x] = len[p] + 1;
for (; p && !ch[p][c]; p = lk[p]) ch[p][c] = x;
if (!p) return lk[x] = 1, void();
int q = ch[p][c];
if (len[q] == len[p] + 1)
return lk[x] = q, void();
int cl = ++nd;
len[cl] = len[p] + 1;
memcpy(ch[cl], ch[q], 104);
lk[cl] = lk[q], lk[q] = lk[x] = cl;
for (; p && ch[p][c] == q; p = lk[p]) ch[p][c] = cl;
}
} // namespace sam
struct Runs {
int l, r, p;
bool operator==(const Runs &j) {
return l == j.l && r == j.r && p == j.p;
}
};
const long long mod1 = 995053212556338707;
const long long bas1 = 315, bas2 = 79;
const int maxn = 4e5 + 5;
char t[maxn];
int m;
namespace getRuns {
struct Hash {
long long p[maxn], h[maxn];
long long mod, buf;
Hash(long long _buf, long long _mod) {
buf = _buf;
mod = _mod;
}
void Init(char *s, int n) {
p[0] = 1;
h[0] = 0;
for (int i = 1; i <= n; i++) {
p[i] = (__int128)p[i - 1] * buf % mod;
h[i] = ((__int128)h[i - 1] * buf + s[i] - 'a' + 1) % mod;
}
}
bool tl(int u, int v, int len) {
return (h[u] - h[v] + (__int128)(h[v - len] - h[u - len]) * p[len]) % mod == 0;
}
bool tr(int u, int v, int len) {
return (h[u + len - 1] - h[v + len - 1] + (__int128)(h[v - 1] - h[u - 1]) * p[len]) % mod == 0;
}
long long get(int l, int r) {
if (l > r) return 0;
return (h[r] + (__int128)(mod - h[l - 1]) * p[r - l + 1] % mod) % mod;
}
} h1(bas1, mod1); // h2(bas2,mod2);
int extr(int x, int y) {
int l = 0, r = m - max(x, y) + 1, res = 0;
while (l <= r) {
int mid = l + r >> 1;
if (h1.tr(x, y, mid)) {
// if (h1.tr(x, y, mid) && h2.tr(x, y, mid)) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
return res;
}
int extl(int x, int y) {
int l = 0, r = min(x, y), res = 0;
while (l <= r) {
int mid = l + r >> 1;
if (h1.tl(x, y, mid)) {
// if (h1.tl(x, y, mid) && h2.tl(x, y, mid)) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
return res;
}
vector<Runs> raw_runs(0);
int CMP(int x, int y) {
if (x == y)
return 0;
int l = extr(x, y);
return t[x + l] < t[y + l] ? -1 : 1;
}
void Check(int i, int j) {
int p = j - i + 1;
int x = i - extl(i - 1, j), y = j + extr(i, j + 1);
if (y - x + 1 >= p * 2)
raw_runs.push_back({x, y, p});
}
int st[maxn], top;
void init() {
for (int i = m; i; i--) {
while (top && CMP(i, st[top]) <= 0)
top--;
if (top)
Check(i, st[top] - 1);
st[++top] = i;
}
top = 0;
for (int i = m; i; i--) {
while (top && CMP(i, st[top]) >= 0)
top--;
if (top)
Check(i, st[top] - 1);
st[++top] = i;
}
}
} // namespace getRuns
void solve() {
cin >> S;
n = strlen(S);
long long ans = 0;
sam ::init();
for (int i = 0; i < n; i++) {
sam ::extend(S[i] - 'a');
t[i + 1] = S[i];
int x = sam ::las;
ans += sam ::len[x] - sam ::len[sam ::lk[x]];
}
m = n;
getRuns::h1.Init(t, m);
// getRuns::h2.Init(t, m);
getRuns::init();
sort(getRuns::raw_runs.begin(), getRuns::raw_runs.end(), [](const Runs &A, const Runs &B) {
return A.l == B.l ? (A.r == B.r ? A.p < B.p : A.r < B.r) : A.l < B.l;
});
int tot = std::unique(getRuns::raw_runs.begin(), getRuns::raw_runs.end()) - getRuns::raw_runs.begin();
getRuns::raw_runs.resize(tot);
unordered_map<long long, int> mp(n * 15);
for (auto [l, r, p] : getRuns::raw_runs) {
for (int i = 0; i < p; i++) {
int t = (r - l + 1 - i) / p - 1;
if (t) {
long long h = getRuns::h1.get(l + i, l + i + p - 1);
int &v = mp[h];
if (t > v) v = t;
} else
break;
}
}
getRuns::raw_runs.clear();
getRuns::top = 0;
for (auto [x, y] : mp) ans -= y;
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 11764kb
input:
6 rikkasuggeststoallthecontestants thisisaproblemdesignedforgrandmasters ifyoudidnotachievethat youdbetterskiptheproblem wishyouahighrank enjoytheexperience
output:
500 679 244 290 132 163
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 5077ms
memory: 91296kb
input:
1000 mxsslrrxtwwnwlsrxxxbsmtxxwwntsbrwlxnbxunutnlmrrlubturuwmbnobrolwunwurtbnmlumxmwtwrsulruxslsnltsstmmxbsmuonroubrnnmu sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss glggglgg yykkyyyykykykyyyykyykykkkyyyykkyykkkyyykykyykyyykkykky...
output:
6522 1 20 9443 11294 1 8619 26 7898 260905 9048 6 22114 52 20 45 7 39 10 1 28 26 10 47 32 12977 30 13 7473 12 8402 1 8083 16 1 10462 16 9278 1 1 8968 7858 11148 8130 6 8516 12223 9266 8374 26 45 14 10150 9 10175 298758 203677 11522 12 8985 10687 12 1 6613277686 10 10 1 5794 28 1 280529 7874 13 10564...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 6061ms
memory: 81372kb
input:
26 hihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihh...
output:
9523687993 9529757593 9537405289 9539347561 9533035177 9528058105 564250 9522959641 9542382361 9518953705 9519439273 9534977449 9519803449 9535705801 9519560665 9534249097 9527572537 9523687993 9539468953 9532064041 9525873049 9532185433 9541168441 9524901913 9531092905 9518832313
result:
ok 26 lines
Test #4:
score: 0
Accepted
time: 5970ms
memory: 81204kb
input:
26 oohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohooh...
output:
9625902365 9628810517 9622671085 9626467839 9628891299 9626306275 9630668503 9620409189 9618228075 9622428739 9628406607 9625336891 9630426157 9626871749 9620086061 9626144711 9616935563 9617177909 9626790967 9627194877 9626467839 354971 9616370089 9618308857 9617824165 9616773999
result:
ok 26 lines
Test #5:
score: 0
Accepted
time: 5469ms
memory: 78496kb
input:
26 vggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvg...
output:
13085098340 13073668733 13071665606 13067070197 13077910649 13074964874 13078735466 13070840789 13072726085 13067895014 669720 13065891887 13065302732 13076496677 13068484169 13064242253 13065420563 13063181774 13080502931 13067070197 13072490423 13070015972 13083802199 13064831408 13075671860 13085...
result:
ok 26 lines