QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#572633 | #5084. Longest Substring | daxinghao# | WA | 103ms | 33596kb | C++14 | 3.8kb | 2024-09-18 15:44:33 | 2024-09-18 15:44:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#else
#endif
#define int long long
#define sz(v) ((int)(v).size())
#define MAX(x, y) (x = max((x), (y)))
#define MIN(x, y) (x = min((x), (y)))
#define pi array<int, 2>
#define all(v) (v).begin(), (v).end()
vector<int> Suffix_array(string s){
int n = (int)s.size(), m = max(300ll, n + 1);
vector<int> sa(n), gr(n * 2), ngr(n * 2), cnt(m), imsi(m);
for(int i = 0; i < n; ++i) sa[i] = i, gr[i] = s[i];
for(int len = 1; len < n; len <<= 1){
for(int i = 0; i < m; ++i) cnt[i] = 0;
for(int i = 0; i < n; ++i) ++cnt[gr[i + len]];
for(int i = 1; i < m; ++i) cnt[i] += cnt[i - 1];
for(int i = n - 1; i >= 0; --i) imsi[--cnt[gr[i + len]]] = i;
for(int i = 0; i < m; ++i) cnt[i] = 0;
for(int i = 0; i < n; ++i) ++cnt[gr[i]];
for(int i = 1; i < m; ++i) cnt[i] += cnt[i - 1];
for(int i = n - 1; i >= 0; --i) sa[--cnt[gr[imsi[i]]]] = imsi[i];
ngr[sa[0]] = 1;
for(int i = 1; i < n; ++i) ngr[sa[i]] = ngr[sa[i - 1]] + (gr[sa[i - 1]] < gr[sa[i]] || (gr[sa[i - 1]] == gr[sa[i]] && gr[sa[i - 1] + len] < gr[sa[i] + len]));
swap(gr, ngr);
if(gr[sa[n - 1]] == n) break;
}
return sa;
}
vector<int> Lcp(string s, vector<int> sa){
int n = (int)s.size(); s += '^';
vector<int> lcp(n - 1), rk(n);
for(int i = 0; i < n; ++i) rk[sa[i]] = i;
for(int i = 0, j = 0; i < n; ++i) if(rk[i]){
while(s[i + j] == s[sa[rk[i] - 1] + j]) ++j;
lcp[rk[i] - 1] = (j ? j-- : 0);
}
return lcp;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string s; cin >> s;
int n = sz(s);
auto sa = Suffix_array(s);
auto lcp = Lcp(s, sa);
vector<int> ans(n + 1);
vector<vector<pi>> spa(n - 1, vector<pi>(18));
for(int i = 0; i < n - 1; ++i){
spa[i][0] = {lcp[i], i};
}
for(int j = 1; j < 18; ++j){
for(int i = 0; i < n - 1; ++i){
spa[i][j] = spa[i][j - 1];
if(i + (1 << j - 1) < n - 1){
MIN(spa[i][j], spa[i + (1 << j - 1)][j - 1]);
}
}
}
auto sol = [&](auto&&self, int s, int e, int ed)->set<int>{
assert(s <= e);
if(s == e){
if(ed < n - sa[s]) MAX(ans[1], n - sa[s]);
set<int> st;
st.insert(sa[s]);
return st;
}
pi mn = {(int)1e9, (int)1e9};
for(int j = 17; j >= 0; --j){
if(s + (1 << j) > e) continue;
MIN(mn, spa[s][j]);
MIN(mn, spa[e - (1 << j)][j]);
break;
}
assert(mn[1] < (int)1e9);
int p = mn[1], tlen = mn[0];
auto st = self(self, s, p, tlen);
auto st2 = self(self, p + 1, e, tlen);
if(sz(st2) > sz(st)) swap(st, st2);
for(auto&v:st2) st.insert(v);
auto getcnt = [&](int gap){
int rv = 1;
int now = *st.begin();
while(true){
auto nxt = st.lower_bound(now + gap);
if(nxt == st.end()) break;
++rv;
now = *nxt;
}
return rv;
};
if(ed < tlen){
int sval = getcnt(ed + 1);
int low = ed + 1, high = tlen, mid;
while(low < high){
mid = low + high + 1 >> 1;
if(getcnt(mid) == sval){
low = mid;
}
else{
high = mid - 1;
}
}
MAX(ans[e - s + 1], low);
}
return st;
};
sol(sol, 0, n - 1, 0);
for(int i = 1; i <= n; ++i){
cout << ans[i] << ' ';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
input:
ababa
output:
5 2 1 0 0
result:
ok single line: '5 2 1 0 0 '
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
aaaaaaaa
output:
8 7 6 5 4 3 2 1
result:
ok single line: '8 7 6 5 4 3 2 1 '
Test #3:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
a
output:
1
result:
ok single line: '1 '
Test #4:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
abcdefghijklmnopqrstuvwxyz
output:
26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok single line: '26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '
Test #5:
score: 0
Accepted
time: 79ms
memory: 33596kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 49951 ...
result:
ok single line: '50000 49999 49998 49997 49996 ... 13 12 11 10 9 8 7 6 5 4 3 2 1 '
Test #6:
score: -100
Wrong Answer
time: 103ms
memory: 29420kb
input:
abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...
output:
50000 49998 49996 49994 49992 49990 49988 49986 49984 49982 49980 49978 49976 49974 49972 49970 49968 49966 49964 49962 49960 49958 49956 49954 49952 49950 49948 49946 49944 49942 49940 49938 49936 49934 49932 49930 49928 49926 49924 49922 49920 49918 49916 49914 49912 49910 49908 49906 49904 49902 ...
result:
wrong answer 1st lines differ - expected: '50000 49998 49996 49994 49992 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '50000 49998 49996 49994 49992 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '