QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#148465#243. Lyndon Substringnameless_story0 124ms16892kbC++203.0kb2023-08-23 16:19:072023-08-23 20:22:51

Judging History

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

  • [2023-08-23 20:22:51]
  • 评测
  • 测评结果:0
  • 用时:124ms
  • 内存:16892kb
  • [2023-08-23 16:19:07]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N 30'0000

void output(vector<LL> &f){
    for (auto x:f) cout<<x<<' '; cout<<endl;
}
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 init(LL n){
    pw[0]=1; for (LL i=1;i<=n;++i) pw[i]=pw[i-1]*B%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);
                    }
                    lst-=cyc;
                }
            }
        }
    }
    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]+1;
        }
        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;
    init(200000);
    while (T--) solve();
}

详细

Test #1:

score: 0
Wrong Answer
time: 124ms
memory: 16892kb

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
6
2
5
3
4
5
3
4
4
4
3
4
2
3
5
7
4
2
2
4
2
8
6
5
4
3
2
6
3
3
6
3
8
5
2
4
3
4
5
3
3
4
2
2
3
6
7
2
2
4
8
2
2
2
7
3
5
3
2
5
4
2
3
3
2
2
3
3
5
2
4
2
3
2
5
3
9
3
5
2
6
3
3
7
2
2
2
3
6
4
3
3
7
5
5
5
3
5
6
6
6
5
8
6
5
5
5
5
7
7
6
7
5
6
5
7
7
5
5
5
5
8
6
5
5
5
5
6
6
5
6
6
8
5
5
7
6
7
5
6
5
6
5
5
6
6
7
...

result:

wrong answer 4th lines differ - expected: '11', found: '6'