UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#200489#2422. 名字wosile100428ms20564kbC++111.8kb2024-01-04 09:00:482024-01-04 12:05:51

answer

#include<bits/stdc++.h>
using namespace std;
char s[100005];
int tr[100005][30],fail[100005],fa[100005];
int dfn[100005],dfr[100005],ed[1005],id[100005];
int n,m,tot,cnt;
queue<int>q;
vector<int>T[100005];
int r[1005][1005],pre[1005][1005];
void dfs(int x){
	dfr[x]=dfn[x]=++cnt;
	// printf("dfn[%d]=%d\n",x,cnt);
	for(int v:T[x]){
		dfs(v);
		dfr[x]=dfr[v];
	}
}
int ft[100005];
inline int lowbit(int x){return x&(-x);}
void add(int x,int v){
	// printf("add %d %d\n",x,v);
	for(;x<=cnt;x+=lowbit(x))ft[x]+=v;
}
int query(int x){
	int ans=0;
	// printf("query %d\n",x);
	for(;x;x-=lowbit(x))ans+=ft[x];
	return ans;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		int len=strlen(s+1);
		int cur=0;
		for(int j=1;j<=len;j++){
			int c=s[j]-'a';
			if(!tr[cur][c]){
				tr[cur][c]=++tot;
				fa[tot]=cur;
			}
			cur=tr[cur][c];
		}
		ed[i]=cur;
		id[cur]=i;
	}
	for(int i=0;i<26;i++)if(tr[0][i])q.push(tr[0][i]);
	while(!q.empty()){
		int f=q.front();
		q.pop();
		// printf("%d\n",f);
		for(int i=0;i<26;i++){
			if(tr[f][i]){
				fail[tr[f][i]]=tr[fail[f]][i];
				// printf("%d %c to %d\n",f,i+'a',tr[f][i]);
				q.push(tr[f][i]);
			}
			else tr[f][i]=tr[fail[f]][i];
		}
	}
	// for(int i=1;i<=tot;i++)printf("fail[%d]=%d\n",i,fail[i]);
	for(int i=1;i<=tot;i++)T[fail[i]].push_back(i);
	dfs(0);
	// cnt=tot+1
	for(int i=1;i<=n;i++){
		int cur=ed[i];
		while(cur){
			add(dfn[cur],1);
			cur=fa[cur];
		}
		for(int j=1;j<=n;j++){
			r[j][i]=query(dfr[ed[j]])-query(dfn[ed[j]]-1);
			// printf("r[%d][%d]=%d\n",j,i,r[j][i]);
			pre[j][i]=pre[j][i-1]+r[j][i];
		}
		cur=ed[i];
		while(cur){
			add(dfn[cur],-1);
			cur=fa[cur];
		}
	}
	while(m--){
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		printf("%d\n",pre[x][r]-pre[x][l-1]);
	}
	return 0;
}
// quod erat demonstrandum

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 2ms
memory: 3708kb

input:

10 100
bc
a
aaa
aaaccaaaca
caaabacbcaaaaccaaaaaccaaacaccaaaa
aaabacacbccbccaaabacbc
accaaaa
aaaccaaa...

output:

12
0
1
48
7
0
56
1
15
3
1
1
49
46
11
18
4
5
13
45
1
4
32
18
1
1
7
10
0
18
1
5
1
1
1
4
53
55
1
24
4
1...

result:

ok 100 lines

Test #2:

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

input:

5 100
ejmljknnjeldejmljknje
ldejmljknnjeldejmljknnjfldejmlj
jmkjknjeld
fldejmljknnjeldejmljknjeldejm...

output:

6
5
1
7
10
5
8
2
6
6
3
8
4
5
4
1
5
4
9
5
7
3
6
5
4
9
5
4
5
3
5
9
9
8
10
5
1
4
3
2
8
4
4
1
1
8
7
5
4
...

result:

ok 100 lines

Test #3:

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

input:

50 1000
aaacaaaabaaaaaaa
a
aaaacaaaabaaaaaaaabaaaa
aaaaaaaaacaaaabaaaaaaaabaaaaaaaaaaa
acbbaaabacaaa...

output:

129
77
0
21
274
11
72
109
26
7
284
18
173
19
349
8
15
5
27
6
51
311
39
371
1
3
44
6
12
492
323
346
3...

result:

ok 1000 lines

Test #4:

score: 10
Accepted
time: 2ms
memory: 3976kb

input:

30 1000
mgenjcfodhnmgenjcfodhnm
genjcfodhnmgenjcfodhnmgenjcfodhnmenjcfodhnmgenjcfodhnmenjcfodhnmgenj...

output:

23
10
77
28
11
25
40
8
56
41
15
0
2
39
2
52
79
4
60
4
10
40
20
84
82
8
0
4
42
24
6
68
23
3
7
10
77
4...

result:

ok 1000 lines

Test #5:

score: 10
Accepted
time: 53ms
memory: 16784kb

input:

500 100000
bcabcbbbcacaabccbccacaccbcacbccccbbaaabbabcbcccaababccbcabcbbbcacaabccbccacaccbcacb
cccbb...

output:

14
3954
226
633
528
2614
257
53
440
148
2144
768
268
17
284
185
234
30
481
495
202
0
1338
1694
4910
...

result:

ok 100000 lines

Test #6:

score: 10
Accepted
time: 55ms
memory: 18440kb

input:

500 100000
ehecbhadefejeehecehecbhadefejeehecehecbhadefejeehecehecbhadefejeehecehecbhadefejeehecehec...

output:

251
669
335
2013
1080
23
196
450
4697
1291
35
32
1332
9
45
877
723
1340
327
138
225
218
376
194
48
1...

result:

ok 100000 lines

Test #7:

score: 10
Accepted
time: 45ms
memory: 19460kb

input:

200 100000
cefdicgefchdcbbfabajbdhcga
efchcgacfahcgacfaaicfbhihciadedhacafcefdicgefchcgacfahcgacfaai...

output:

6
150
1
380
125
482
75
560
7
180
13
0
134
41
5
599
299
6
23
108
0
171
212
220
39
136
9
62
100
132
8
...

result:

ok 100000 lines

Test #8:

score: 10
Accepted
time: 92ms
memory: 18120kb

input:

500 100000
qhyovpqmdmxzpedsswqrydaogpfjitkxfowgmxglflchcnxzjkcfqhylvpqmdmxzpedsswqrydaogpfjitkxfowgm...

output:

206
393
721
78
191
1162
84
3302
1404
918
31
57
62
234
27
150
2744
1260
88
71
193
208
1437
853
2093
4...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 94ms
memory: 20564kb

input:

200 100000
jjidrjrmjjhkrxsraaaqduplrcznisidrjrmjjidxjrmjjrxsraaaqaaqduslrcznisidrjrmjjidrjrmjjhkrxsr...

output:

6
390
975
291
3
136
6
264
160
125
98
1794
1
17
493
0
2
310
40
1589
11
3
81
146
13
450
5
216
11
66
43...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 85ms
memory: 19024kb

input:

1000 100000
cbabbcbbbbcbbbccacbabbcbbbbcbbbccacbabbcbbbbcbbbccacbabbcbbbbcbbbccacbabbcbb
bccacb
babb...

output:

23
3036
159
187
11472
909
2
257
173
96
1070
21215
459
920
2680
7901
17396
513
3992
1460
31684
4245
2...

result:

ok 100000 lines

Extra Test:

score: 0
Extra Test Passed