QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#265909#2343. First of Her NameInfinityNS#AC ✓297ms255944kbC++141.7kb2023-11-25 22:22:172023-11-25 22:22:17

Judging History

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

  • [2023-11-25 22:22:17]
  • 评测
  • 测评结果:AC
  • 用时:297ms
  • 内存:255944kb
  • [2023-11-25 22:22:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back

const int N=1000050;

int go[N][26],link[N],root=1,tsz=1;
vector<int> C[N];
int cnt[N],node[N];

int AddStr(char*s,int len,int idx){
    int c=root;
    for(int i=1;i<=len;i++){
        if(go[c][s[i]-'A']==0){
            go[c][s[i]-'A']=++tsz;
        }
        c=go[c][s[i]-'A'];
    }
    return c;
}

void Build(){
    queue<int> q;
    link[root]=root;
    for(int i=0;i<26;i++){
        if(go[root][i]){
            q.push(go[root][i]);
            link[go[root][i]]=root;
        }else{
            go[root][i]=root;
        }
    }
    while(q.size()){
        int u=q.front();
        q.pop();
        for(int i=0;i<26;i++){
            if(go[u][i]){
                link[go[u][i]]=go[link[u]][i];
                q.push(go[u][i]);
            }else{
                go[u][i]=go[link[u]][i];
            }
        }
    }
    for(int i=2;i<=tsz;i++)C[link[i]].pb(i);
}

char chr[N];
char s[N];
vector<int> E[N];

void DFS(int u,int p,int c){
    c=go[c][chr[u]-'A'];
    cnt[c]++;
    for(int v:E[u]){
        DFS(v,u,c);
    }
}
void Solve(int u){
    for(int v:C[u]){
        Solve(v);
        cnt[u]+=cnt[v];
    }
}

int main(){
    int n,k;
    scanf("%i %i",&n,&k);
    int rt=0;
    for(int i=1;i<=n;i++){
        int p;
        scanf("\n%c %i",&chr[i],&p);
        if(p==0)rt=i;
        else E[p].pb(i);
    }
    for(int i=1;i<=k;i++){
        scanf("%s",s+1);
        int m=strlen(s+1);
        reverse(s+1,s+1+m);
        node[i]=AddStr(s,m,i);
    }
    Build();
    DFS(rt,0,root);
    Solve(root);
    for(int i=1;i<=k;i++){
        printf("%i\n",cnt[node[i]]);
    }
    return 0;
}

Details

Test #1:

score: 100
Accepted
time: 3ms
memory: 58536kb

Test #2:

score: 0
Accepted
time: 6ms
memory: 56824kb

Test #3:

score: 0
Accepted
time: 9ms
memory: 56864kb

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

score: 0
Accepted
time: 4ms
memory: 56844kb

Test #8:

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

Test #9:

score: 0
Accepted
time: 4ms
memory: 57932kb

Test #10:

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

Test #11:

score: 0
Accepted
time: 194ms
memory: 255944kb

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

score: 0
Accepted
time: 4ms
memory: 56432kb

Test #16:

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

Test #17:

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

Test #18:

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

Test #19:

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

Test #20:

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

Test #21:

score: 0
Accepted
time: 4ms
memory: 58208kb

Test #22:

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

Test #23:

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

Test #24:

score: 0
Accepted
time: 4ms
memory: 59184kb

Test #25:

score: 0
Accepted
time: 4ms
memory: 57316kb

Test #26:

score: 0
Accepted
time: 12ms
memory: 58196kb

Test #27:

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

Test #28:

score: 0
Accepted
time: 15ms
memory: 57576kb

Test #29:

score: 0
Accepted
time: 12ms
memory: 58384kb

Test #30:

score: 0
Accepted
time: 8ms
memory: 58352kb

Test #31:

score: 0
Accepted
time: 4ms
memory: 60036kb

Test #32:

score: 0
Accepted
time: 10ms
memory: 64340kb

Test #33:

score: 0
Accepted
time: 12ms
memory: 64520kb

Test #34:

score: 0
Accepted
time: 103ms
memory: 118868kb

Test #35:

score: 0
Accepted
time: 198ms
memory: 78460kb

Test #36:

score: 0
Accepted
time: 297ms
memory: 183500kb

Test #37:

score: 0
Accepted
time: 120ms
memory: 145752kb

Test #38:

score: 0
Accepted
time: 140ms
memory: 66052kb

Test #39:

score: 0
Accepted
time: 158ms
memory: 222432kb

Test #40:

score: 0
Accepted
time: 165ms
memory: 217836kb

Test #41:

score: 0
Accepted
time: 110ms
memory: 109860kb

Test #42:

score: 0
Accepted
time: 184ms
memory: 214656kb

Test #43:

score: 0
Accepted
time: 108ms
memory: 117768kb

Test #44:

score: 0
Accepted
time: 169ms
memory: 119716kb

Test #45:

score: 0
Accepted
time: 234ms
memory: 68436kb

Test #46:

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

Test #47:

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

Test #48:

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

Test #49:

score: 0
Accepted
time: 4ms
memory: 57096kb

Test #50:

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

Test #51:

score: 0
Accepted
time: 11ms
memory: 57224kb

Test #52:

score: 0
Accepted
time: 4ms
memory: 57992kb

Test #53:

score: 0
Accepted
time: 4ms
memory: 58368kb

Test #54:

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

Test #55:

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

Test #56:

score: 0
Accepted
time: 18ms
memory: 57344kb