「tjoi2013」单词

Source and Judge

TJOI2013
luogu3966

Record

1h

Analysis

请先思考后再展开

题意有点晦涩,就是求每个字符串在别的字符串中出现次数只和+1
跑ac机,建出fail树,每个点的点权为被多少个字符串经过,则答案为每个字符串结尾,fail树上子树权值和

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


#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef DEBUG
const int LOCAL=1;
#else
const int LOCAL=0;
#endif

namespace mine
{
typedef long long ll;
const int INF=0x3f3f3f3f;
int ()
{
int ans=0,f=1;char c=getchar();
while(c<'0' or c>'9') {if(c=='-') f=-1;;c=getchar();}
while(c>='0' and c<='9') ans=ans*10+c-'0',c=getchar();
return ans*f;
}

const int MAX_N=1e6+10;
char str[MAX_N];
int pos[210];
struct ACM
{
struct Nod
{
int son[26];
int siz,fail;
void clear()
{
siz=fail=0;
memset(son,0,sizeof son);
}
}p[MAX_N];
int id;
ACM() {id=0;p[0].clear();}

void insert(int pp)
{
int ln=strlen(str+1);
int now=0;
for(int t=1;t<=ln;t++)
{
int wt=str[t]-'a';
if(p[now].son[wt]==0)
{
p[now].son[wt]=++id;
p[id].clear();
}
now=p[now].son[wt];
p[now].siz++;
}
pos[pp]=now;
}
int q[MAX_N];//不清楚的队列
void buildfail()
{
int tou=1,wei=0;
q[++wei]=0;
while(tou<=wei)
{
int fa=q[tou++];
for(int wt=0;wt<26;wt++)
{
int x=p[fa].son[wt];if(x==0) continue;

int lst=p[fa].fail;
while(lst>0 and !p[lst].son[wt]) lst=p[lst].fail;
if(p[lst].son[wt]!=x) p[x].fail=p[lst].son[wt];
else p[x].fail=0;

q[++wei]=x;
}
}
for(int t=wei;t>=2;t--) p[p[q[t]].fail].siz+=p[q[t]].siz;//不需要真的建树
}
}acm;
void main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",str+1);
acm.insert(i);
}
acm.buildfail();
for(int i=1;i<=n;i++) printf("%dn",acm.p[pos[i]].siz);
}
};
int main()
{
srand(time(0));
mine::main();
}