QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#148412#243. Lyndon Substringnameless_story0 0ms0kbC++202.8kb2023-08-23 16:05:102023-08-23 20:22:10

Judging History

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

  • [2023-08-23 20:22:10]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-08-23 16:05:10]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 120000

const LL B=233,mod=998244353;
LL pw[N];
struct hashstring{
    vector<LL> f;
    void build(string s){
        f.resize(s.size()+1);
        f[0]=0;
        for (LL i=0;i<s.size();++i){
            auto w=s[i];
            f[i]=(f[i-1]*B+w)%mod;
        }
    }
    LL calc(LL i,LL len){
        return (f[i+len]-f[i]*pw[len])%mod;
    }
};
void solve(){
    LL n,m; cin>>n>>m;
    vector<string> s(n);
    vector<hashstring> f(n);
    for (LL i=0;i<n;++i){
        cin>>s[i];
        f[i].build(s[i]);
    }
    vector<vector<LL>> lyndon(n);
    for (LL i=0;i<n;++i){
        auto st=s[i]+'0';
        for (LL lst=-1,cyc=1,j=1;j<st.size();++j){
            if (st[j]>st[j-cyc]) cyc=j-lst;
            else{
                if (st[j]<st[j-cyc]){
                    for (lst+=cyc;lst<j;lst+=cyc){
                        lyndon[i].push_back(lst);
                    }
                }
            }
        }
    }
    vector<LL> ans(n);
    for (LL i=0;i<n;++i){
        LL lst=0;
        for (LL x:lyndon[i]){
            ans[i]=max(ans[i],x-lst);
            lst=x;
        }
    }
    auto calc1=[&](LL i,LL p,LL len){
        return f[i].calc(p,len);
    };
    auto calc2=[&](LL i,LL j,LL p,LL len){
        if (p+len<=s[i].size()) return f[i].calc(p,len);
        LL t=s[i].size()-p;
        return (f[i].calc(p,t)*pw[len-t]+f[j].calc(0,len-t))%mod;
    };
    auto check=[&](LL i,LL j,LL p1,LL p2,LL len){
        if (calc2(i,j,p1,len)==calc1(j,p2,len)) return false;
        LL l=1,r=len,k=0;
        while (l<=r){
            LL mid=(l+r)>>1;
            if (calc2(i,j,p1,mid)==calc1(j,p2,mid)){
                k=mid;
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }
        if (p1+k<s[i].size()){
            return s[i][p1+k]<s[j][p2+k];
        }
        else{
            return s[j][p1+k-s[i].size()]<s[j][p2+k];
        }
    };
    while (m--){
        LL i,j; cin>>i>>j; --i; --j;
        LL tmp=max(ans[i],ans[j]);
        LL t=0;
        if (lyndon[i].size()>1){
            t=lyndon[i][lyndon[i].size()-2];
        }
        LL l=0,r=lyndon[j].size()-1,k=0;
        while (l<=r){
            LL mid=(l+r)>>1;
            LL L=mid==0?0:lyndon[j][mid-1]+1;
            LL R=lyndon[j][mid];
            if (check(i,j,t,L,R-L+1)){
                k=lyndon[j][mid]+1;
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }
        tmp=max(tmp,(LL)s[i].size()-t+k);
        cout<<tmp<<'\n';
    }
}
int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    LL T; cin>>T;
    while (T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

24
100 10000
ab
babbab
baabbbb
abbababbb
bb
bbbaaaba
b
aababaa
aaabbb
abbabbaaa
aaaab
bbab
bbabb
baaa
bbabbaaba
abaa
b
aabbbb
babbbbbb
bbaaba
aba
bbb
abbbaabaa
aa
bbaabaabbb
abbbbbb
aabbbaabba
ababba
aabbaab
ababa
aabbabbaab
b
aabaabaab
baaababa
babaaa
abaaaabbbb
aaaaabaa
bbb
bbab
baaa
bbabba
ababbb...

output:

2
3
6
4
4
5
3
4
5
3
4
4
4
3
4
2
3
5
10
4
2
5
3
2
4
6
4
4
3
2
6
3
3
6
3
8
5
5
4
3
4
5
3
3
3
2
2
3
9
7
2
2
4
8
2
2
5
7
3
5
3
2
5
4
4
3
3
2
2
3
3
5
2
4
2
3
2
4
3
9
3
4
2
9
3
3
7
2
2
2
5
6
4
3
3
5
8
5
5
3
3
3
6
4
3
4
3
4
5
3
4
3
3
3
3
3
3
5
3
3
3
3
3
3
3
6
4
4
3
3
6
3
3
6
3
8
5
3
3
3
3
5
3
3
3
3
3
3
3
7...

result: