QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#698098#5084. Longest SubstringAMATSUKAZE#WA 119ms18992kbC++205.7kb2024-11-01 17:23:212024-11-01 17:23:23

Judging History

你现在查看的是最新测评结果

  • [2024-11-01 17:23:23]
  • 评测
  • 测评结果:WA
  • 用时:119ms
  • 内存:18992kb
  • [2024-11-01 17:23:21]
  • 提交

answer


#include<bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define all(v) begin(v),end(v)
using namespace std;
using ll = long long;
bool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b){ return a < b ? a = b, 1 : 0; }

auto SA(string s){
    ll n = (int)s.size() + 1, lim = 256;
    vector<ll> sa(n), lcp(n), x(all(s) + 1), y(n), ws(max(n, lim)), rk(n);
    iota(all(sa), 0);
    for (ll j =0, p = 0; p<n; j= max(1LL,j*2), lim = p) {
        p = j;
        iota(all(y), n-j);
        rep(i,0,n) if(sa[i] >= j) y[p++] = sa[i] - j;
        fill(all(ws), 0);
        rep(i,0,n) ws[x[i]]++;
        rep(i,1,lim)ws[i] +=ws[i-1];
        for(ll i=n; i--;) sa[--ws[x[y[i]]]] = y[i];
        swap(x, y);
        p=1;
        x[sa[0]] =0;
        rep(i,1,n){
            ll a=sa[i-1],b=sa[i];
            x[b] = (y[a]==y[b] && y[a+j]==y[b+j])?p-1:p++;
        }
    }
    rep(i,1,n)rk[sa[i]] = i;
    for(ll i=0,k=0;i<n-1;lcp[rk[i++]]=k){
        if(k)k--;
        while(s[i+k]==s[sa[rk[i]-1]+k])k++;
    }
    sa.erase(begin(sa));
    lcp.erase(begin(lcp));
    return pair{sa,lcp};
}


template<class S, auto op, auto e> struct segtree {
    int n, sz;
    vector<S> d;
    void upd(int k) { d[k] = op(d[2*k],d[2*k+1]); }
    segtree(int _n = 0) : segtree(vector<S>(_n, e())) {}
    segtree(vector<S> v) : n(v.size()) {
        sz = bit_ceil((uint32_t)n);
        d = vector<S>(sz*2,e());
        rep(i,0,n) d[sz+i] = v[i];
        for (int i = sz-1; i >= 1; i--) upd(i);
    }
    S prod(int l, int r){
        S sml = e(), smr = e();
        l += sz, r += sz;
        while (l < r){
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml,smr);
    }
    void set(int p, S x){
        p += sz;
        d[p] = x;
        while (p >>= 1) upd(p);
    }
    S get(int p){ return d[p + sz]; }
    int max_right(int l, auto f){
        if (l == n) return n;
        l += sz;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm,d[l]))){
                while (l < sz){
                    l <<= 1;
                    if (f(op(sm,d[l]))){ sm = op(sm, d[l++]); }
                }
                return l - sz;
            }
            sm = op(sm, d[l++]);
        }while((l & -l) != l);
        return n;
    }
    int min_left(int r, auto f){
        if (r == 0) return 0;
        r += sz;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r],sm))){
                while (r < sz){
                    r = 2*r + 1;
                    if (f(op(d[r],sm))){ sm = op(d[r--],sm); }
                }
                return r + 1 - sz;
            }
            sm = op(d[r], sm);
        }while((r & -r) != r);
        return 0;
    }
};

struct S {
    int val, ind;
};

S op(S a , S b){
    if (a.val < b.val) return a;
    if (a.val > b.val) return b;
    return a;
}

S e() {
    return S{(int)1e9, -1};
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    const char* delim = "";
    for (auto &e : v){
        os << exchange(delim, " ") << e;
    }
    return os;
}
int main(){
    cin.tie(0)->sync_with_stdio(0);

    string s; cin >> s;
    int n = s.size();
    auto [sa,lcp] = SA(s);

    //cout << sa << endl;
    //cout << lcp<< endl;
    
    vector<S> init(n-1);
    rep(i,0,n-1) init[i] = S{(int)lcp[i+1], i};
    segtree<S,op,e>seg(init);
    
    set<int> st_init;
    rep(i,0,n){
        st_init.insert(i);
    }

    vector<int> ans(n+1, 0);
    vector<int> d_max(n+1, -1);

    auto check = [&](set<int> &st, int len) -> int {
        assert((int)st.size() >= 1);
        auto itr = st.begin();
        int cnt = 0;
        while (itr != st.end()) {
            cnt += 1;
            int now = *itr;
            itr =  st.lower_bound(now + len);
       }
       return cnt;
    };

    auto calc = [&](auto self, int l, int r, int level, set<int> &st) -> void {
        S ret = seg.prod(l,r-1);
        int m = ret.ind;
        //cout << l << ' ' << r << ' ' << level << ' ' << (int)st.size() << endl;
        if (ret.val != level) {
            assert(ret.val + 1 > level + 1);
            int init_val = check(st, level + 1);
            int lb = level + 1;
            int ub = ret.val + 1;
            while (ub - lb > 1) {
                int t = (ub + lb) / 2;
                if (check(st, t) == init_val) {
                    lb = t;
                }else{
                    ub = t;
                }
            }
            
            if (d_max[r-l] < init_val) {
                ans[r-l] = lb;
            }else if(d_max[r-l] == init_val) {
                chmax(ans[r-l], lb);
            }
        }

        if (l + 1 == r) return;

        set<int> st_sub;
        int l1 = l;
        int r1 = m+1;
        int l2 = m+1;
        int r2 = r;
        if (r1-l1 < r2-l2) {
            rep(i,l1,r1){
                st.erase(sa[i]);
                st_sub.insert(sa[i]);
            }
            self(self, l1, r1, ret.val, st_sub);
            self(self, l2, r2, ret.val, st);
        }else{
            rep(i,l2,r2){
                st.erase(sa[i]);
                st_sub.insert(sa[i]);
            }
            self(self, l2, r2, ret.val, st_sub);
            self(self, l1, r1, ret.val, st);
        }
    };    

    calc(calc, 0, n, 0, st_init);

    ans[1] = n;

    rep(i,1,n+1){
        cout << ans[i];
        if (i == n) cout << '\n';
        else cout << ' ';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

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: 3536kb

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: 3852kb

input:

a

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3648kb

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: 88ms
memory: 18992kb

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 ...4 13 12 11 10 9 8 7 6 5 4 3 2 1'

Test #6:

score: -100
Wrong Answer
time: 119ms
memory: 13564kb

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 0'