1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
   | #include<bits/stdc++.h> using namespace std; int T; int n; int aid[200001]; struct node{ 	int num; 	int ch[26]; 	int fail; 	int rd; }tree[2000001]; int tot; string s; inline void build(int now) { 	cin>>s; 	int x=0; 	for(int i=0;i<s.length();i++) 	{ 		int c=s[i]-'a'; 		if(!tree[x].ch[c]) 		{ 			tree[x].ch[c]=++tot; 		} 		x=tree[x].ch[c]; 	} 	aid[now]=x; 	return ; } queue<int> q; inline void build_failtree() { 	for(int i=0;i<=25;i++) 	{ 		if(tree[0].ch[i]) 		{ 			q.push(tree[0].ch[i]); 		} 	} 	while(!q.empty()) 	{ 		int x=q.front(); 		q.pop(); 		for(int i=0;i<=25;i++) 		{ 			if(tree[x].ch[i]) 			{ 				q.push(tree[x].ch[i]); 			} 		} 		for(int i=0;i<=25;i++) 		{ 			if(tree[tree[x].fail].ch[i]) 			{ 				if(tree[x].ch[i]) 				{ 					tree[tree[x].ch[i]].fail=tree[tree[x].fail].ch[i]; 					tree[tree[tree[x].fail].ch[i]].rd++; 				} 				else 				{ 					tree[x].ch[i]=tree[tree[x].fail].ch[i]; 				} 			} 		} 	} 	return ; } inline void query() { 	int x=0; 	cin>>s; 	for(int i=0;i<s.length();i++) 	{ 		int c=s[i]-'a'; 		x=tree[x].ch[c]; 		tree[x].num++; 	} 	return ; } int main() { 	cin>>n; 	for(int i=1;i<=n;i++) 	{ 		build(i); 	} 	build_failtree(); 	query(); 	for(int i=1;i<=tot;i++) 	{ 		if(!tree[i].rd) 		{ 			q.push(i); 		} 	} 	while(!q.empty()) 	{ 		int x=q.front(); 		q.pop(); 		tree[tree[x].fail].num+=tree[x].num; 		tree[tree[x].fail].rd--; 		if(!tree[tree[x].fail].rd) 		{ 			q.push(tree[x].fail); 		} 	} 	for(int i=1;i<=n;i++) 	{ 		cout<<tree[aid[i]].num<<endl; 	} 	return 0; }
   |