QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#549925#2343. First of Her NameAllSolvedin1557RE 398ms118392kbC++172.2kb2024-09-06 23:45:182024-09-06 23:45:19

Judging History

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

  • [2024-09-06 23:45:19]
  • 评测
  • 测评结果:RE
  • 用时:398ms
  • 内存:118392kb
  • [2024-09-06 23:45:18]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef long double ld;


const int MX = 1e6;
int gr[MX+3],sa[MX+3],tmp[MX+3],cnt[MX+3];
int st[22][MX+3];
int len[MX+3];
pair<char,int> A[MX+3];

int n,c;

bool cmp(int x,int y)
{
    if(gr[x]!=gr[y]) return gr[x]<gr[y];
    if(st[c][x]==n||st[c][y]==n) return len[x]<len[y];
    return gr[st[c][x]]<gr[st[c][y]];
}
void counting_sort()
{
    int sz=max(30,n);
    fill(cnt,cnt+sz,0);
    for(int i=0;i<n;i++) cnt[gr[st[c][i]]+1]++;
    for(int i=1;i<sz;i++) cnt[i]+=cnt[i-1];
    for(int i=n-1;i>=0;i--) tmp[--cnt[gr[st[c][i]]+1]]=i;
    fill(cnt,cnt+sz,0);
    for(int i=0;i<n;i++) cnt[gr[i]]++;
    for(int i=1;i<sz;i++) cnt[i]+=cnt[i-1];
    for(int i=n-1;i>=0;i--) sa[--cnt[gr[tmp[i]]]]=tmp[i];
}
void buildsa()
{
    for(int i=0;i<n;i++) gr[i]=A[i].fi-'A'+1, sa[i]=i;
    for(c=0; ; c++)
    {
        counting_sort();
        fill(tmp,tmp+n,0);
        for(int i=1;i<n;i++) tmp[i]=tmp[i-1]+cmp(sa[i-1],sa[i]);
        for(int i=0;i<n;i++) gr[sa[i]]=tmp[i];
        if(tmp[n-1]==n-1) break;
    }
}
bool gre(string &s,int x) // s<=x 인가?
{
    int now=sa[x];
    for(int i=0;i<min((int)s.size(),len[sa[x]]);i++)
    {
        if(s[i]!=A[now].fi) return s[i]<A[now].fi;
        now=A[now].se;
    }
    return s.size()<=len[sa[x]];
}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int q; cin>>n>>q;
    for(int i=0;i<n;i++)
    {
        string s; int x; cin>>s>>x;
        if(x==0) x=n;
        else x--;
        st[0][i]=x; A[i]={s[0],x}; len[i]=len[x]+1;
    }
    st[0][n]=n;
    for(int j=1;j<22;j++) for(int i=0;i<=n;i++) st[j][i]=st[j-1][st[j-1][i]];
    buildsa();
    while(q--)
    {
        string s; cin>>s;
        int l=0,r=n;
        while(l<r)
        {
            int m=(l+r)/2;
            if(gre(s,m)) r=m;
            else l=m+1;
        }
        s.back()++;
        int a=l;
            l=0; r=n;
            while(l<r)
            {
                int m=(l+r)/2;
                if(gre(s,m)) r=m;
                else l=m+1;
            }
        cout<<r-a<<'\n';
    }
    return 0;
}

详细

Test #1:

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

Test #2:

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

Test #3:

score: 0
Accepted
time: 3ms
memory: 56836kb

Test #4:

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

Test #5:

score: 0
Accepted
time: 3ms
memory: 56880kb

Test #6:

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

Test #7:

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

Test #8:

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

Test #9:

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

Test #10:

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

Test #11:

score: 0
Accepted
time: 398ms
memory: 118392kb

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

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

Test #16:

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

Test #17:

score: 0
Accepted
time: 3ms
memory: 56816kb

Test #18:

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

Test #19:

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

Test #20:

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

Test #21:

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

Test #22:

score: 0
Accepted
time: 3ms
memory: 56808kb

Test #23:

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

Test #24:

score: 0
Accepted
time: 7ms
memory: 57228kb

Test #25:

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

Test #26:

score: -100
Runtime Error