QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200716#2343. First of Her NameHIR180TL 450ms282976kbC++141.9kb2023-10-04 18:55:052023-10-04 18:55:05

Judging History

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

  • [2023-10-04 18:55:05]
  • 评测
  • 测评结果:TL
  • 用时:450ms
  • 内存:282976kb
  • [2023-10-04 18:55:05]
  • 提交

answer

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

using ll = long long;
#define rng(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,b) rng(i,0,b)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define si(x) (int)(x.size())

#define tct template<class t>
#define tctu template<class t, class u>

tct using vc=vector<t>;
using vi=vc<int>;
tctu bool chmax(t&a, u b){ if(a<b){a=b;return 1;}return 0;}
tctu bool chmin(t&a, u b){ if(a>b){a=b;return 1;}return 0;}
#define a first
#define b second

struct Aho{
	struct N{
		map<char,int>nx;
		int fail;
		N():fail(-1){}
	};
	vc<N>x;
	vi idx,pos;
	vc<vi>edge;
	vi sum;
	Aho():x(1){}
	void make(int up){
		idx.pb(0);
		int h=0;
		while(h<si(idx)){
			int i=idx[h++];
			for(auto e:x[i].nx){
				int j=e.b,f=x[i].fail;
				while(f!=-1 and x[f].nx.count(e.a)==0)
					f=x[f].fail;
				if(f != -1)
					x[j].fail=x[f].nx[e.a];
				else
					x[j].fail=0;
				idx.pb(j);
			}
		}
		edge.resize(si(idx));
		sum.resize(si(idx));
		rep(i,up)sum[i]++;
		rep(i,si(idx)){
			if(x[i].fail < 0) continue;
			edge[x[i].fail].pb(i);
		}
		auto dfs=[&](auto self,int v)->void{
			for(auto to:edge[v]){
				self(self,to);
				sum[v]+=sum[to];
			}
		};
		dfs(dfs,0);
	}
	N& operator[](int i){return x[i];}
};

void solve(){
	int n,k;cin>>n>>k;
	Aho ah;
	rng(i,1,n+1){
		string s; int p;
		cin>>s>>p;
		if(ah.x[p].nx.count(s[0]) == 0){
			ah.x[p].nx[s[0]]=si(ah.x);
			ah.x.eb();
		}
	}
	vc<string>v(k);
	vi id(k);
	rep(i,k){
		cin>>v[i];
		reverse(all(v[i]));
		int cur = 0;
		rep(j,si(v[i])){
			if(ah.x[cur].nx.count(v[i][j])==0){
				ah.x[cur].nx[v[i][j]]=si(ah.x);
				ah.x.eb();
			}
			cur = ah.x[cur].nx[v[i][j]];
		}
		id[i] = cur;
	}
	ah.make(n+1);
	rep(i,k)cout<<ah.sum[id[i]]<<'\n';
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	solve();
}

Details

Test #1:

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

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

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

Test #8:

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

Test #9:

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

Test #10:

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

Test #11:

score: 0
Accepted
time: 172ms
memory: 197844kb

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

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

Test #16:

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

Test #17:

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

Test #18:

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

Test #19:

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

Test #20:

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

Test #21:

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

Test #22:

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

Test #23:

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

Test #24:

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

Test #25:

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

Test #26:

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

Test #27:

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

Test #28:

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

Test #29:

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

Test #30:

score: 0
Accepted
time: 17ms
memory: 4596kb

Test #31:

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

Test #32:

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

Test #33:

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

Test #34:

score: 0
Accepted
time: 154ms
memory: 199340kb

Test #35:

score: 0
Accepted
time: 298ms
memory: 149104kb

Test #36:

score: 0
Accepted
time: 450ms
memory: 282976kb

Test #37:

score: -100
Time Limit Exceeded