QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#380087#2343. First of Her Namedistant_yesterday#AC ✓480ms281984kbC++201.5kb2024-04-06 20:57:012024-04-06 20:57:05

Judging History

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

  • [2024-04-06 20:57:05]
  • 评测
  • 测评结果:AC
  • 用时:480ms
  • 内存:281984kb
  • [2024-04-06 20:57:01]
  • 提交

answer

// 广义后缀自动机 https://loj.ac/s/1856833
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,k,pos[N],qry[N],ans[N];
string s;
struct ACAM{
	int cntp,tr[2*N][30],fail[2*N];
	struct fail_tree{
		int val[2*N];
		int id,h[2*N],e[2*N],ne[2*N];
		void add(int a,int b){
			id++;
			ne[id]=h[a];
			h[a]=id;
			e[id]=b;
		}
		void dfs(int x){
			for(int i=h[x];i;i=ne[i]){
				dfs(e[i]);
				val[x]+=val[e[i]];
			}
		}
	}ft;
	void update(int p,int c,int id){
		pos[id]=tr[p][c]=++cntp;
		ft.val[cntp]++;
	}
	void insert(string s,int id){
		int p=0;
		for(int i=0;i<int(s.size());i++){
			if(!tr[p][s[i]-'A']){
				tr[p][s[i]-'A']=++cntp;
			}
			p=tr[p][s[i]-'A'];
		}
		qry[id]=p;
	}
	void build(){
		queue<int> que;
		que.push(0);
		while(que.size()){
			int p=que.front();
			que.pop();
			for(int i=0;i<26;i++){
				if(tr[p][i]){
					if(p){
						fail[tr[p][i]]=tr[fail[p]][i];
					}
					ft.add(fail[tr[p][i]],tr[p][i]);
					que.push(tr[p][i]);
				}else{
					tr[p][i]=tr[fail[p]][i];
				}
			}
		}
	}
	void query(){
		ft.dfs(0);
		for(int i=1;i<=k;i++){
			ans[i]=ft.val[qry[i]];
		}
	}
}acam;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		char c;
		int fa;
		scanf("\n%c %d",&c,&fa);
		acam.update(pos[fa],c-'A',i);
	}
	for(int i=1;i<=k;i++){
		cin>>s;
		reverse(s.begin(),s.end());
		acam.insert(s,i);
	}
	acam.build();
	acam.query();
	for(int i=1;i<=k;i++){
		printf("%d\n",ans[i]);
	}
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 2ms
memory: 14292kb

Test #2:

score: 0
Accepted
time: 2ms
memory: 14088kb

Test #3:

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

Test #4:

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

Test #5:

score: 0
Accepted
time: 2ms
memory: 14000kb

Test #6:

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

Test #7:

score: 0
Accepted
time: 2ms
memory: 14080kb

Test #8:

score: 0
Accepted
time: 2ms
memory: 14380kb

Test #9:

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

Test #10:

score: 0
Accepted
time: 2ms
memory: 14240kb

Test #11:

score: 0
Accepted
time: 128ms
memory: 175680kb

Test #12:

score: 0
Accepted
time: 2ms
memory: 14056kb

Test #13:

score: 0
Accepted
time: 2ms
memory: 14084kb

Test #14:

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

Test #15:

score: 0
Accepted
time: 2ms
memory: 14308kb

Test #16:

score: 0
Accepted
time: 2ms
memory: 14340kb

Test #17:

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

Test #18:

score: 0
Accepted
time: 2ms
memory: 14320kb

Test #19:

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

Test #20:

score: 0
Accepted
time: 2ms
memory: 14048kb

Test #21:

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

Test #22:

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

Test #23:

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

Test #24:

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

Test #25:

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

Test #26:

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

Test #27:

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

Test #28:

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

Test #29:

score: 0
Accepted
time: 2ms
memory: 18808kb

Test #30:

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

Test #31:

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

Test #32:

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

Test #33:

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

Test #34:

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

Test #35:

score: 0
Accepted
time: 352ms
memory: 157820kb

Test #36:

score: 0
Accepted
time: 480ms
memory: 281984kb

Test #37:

score: 0
Accepted
time: 133ms
memory: 161180kb

Test #38:

score: 0
Accepted
time: 190ms
memory: 155808kb

Test #39:

score: 0
Accepted
time: 122ms
memory: 174436kb

Test #40:

score: 0
Accepted
time: 146ms
memory: 173016kb

Test #41:

score: 0
Accepted
time: 150ms
memory: 170632kb

Test #42:

score: 0
Accepted
time: 178ms
memory: 223668kb

Test #43:

score: 0
Accepted
time: 137ms
memory: 154788kb

Test #44:

score: 0
Accepted
time: 210ms
memory: 179228kb

Test #45:

score: 0
Accepted
time: 344ms
memory: 158460kb

Test #46:

score: 0
Accepted
time: 2ms
memory: 14120kb

Test #47:

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

Test #48:

score: 0
Accepted
time: 2ms
memory: 14032kb

Test #49:

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

Test #50:

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

Test #51:

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

Test #52:

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

Test #53:

score: 0
Accepted
time: 2ms
memory: 15672kb

Test #54:

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

Test #55:

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

Test #56:

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