bzoj 3473 后缀自动机多字符串的子串处理方法

后缀自动机处理多字符串字串相关问题。

首先,和后缀数组一样,用分割符连接各字符串,然后建一个后缀自动机。

我们定义一个节点代表的字符串为它原本代表的所有串去除包含分割符后的串。每个节点代表的字符串的数量可以用DP来计算(不能用right集合来算了)。

对于原来n个串中的一个串,其所有前缀可以通过将该串放到自动机上跑来获得,对于某个前缀,其所有后缀包括在该前缀本身的节点以及parent树的祖先节点中。这样我们就获得访问某个串所有子串的技能了。

对于这道题,我们可以先建出后缀自动机,然后对于n个串中的每个串,找到包含其子串的所有节点(可以保证所有子串一定且唯一出现在某个节点中)。然后将它们的计数器+1。弄完后,对于每个节点,我们就可以知道其代表的串是n个串中多少个串的子串。

最后再对于每个串,找出所有字串(不同位置要区分),统计答案。

如果不同位置不区分,那么我们得到的节点不能重复。如果要区分,对于每个前缀,其parent树上的节点都要计算,即使以前被计算过(因为他们的结束位置不同,所以肯定要计算)。

 /**************************************************************
Problem: 3473
User: idy002
Language: C++
Result: Accepted
Time:728 ms
Memory:120928 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#define N 200010
#define S 500010
#define P 18
using namespace std; typedef long long dnt; int n, k;
char buf[N], *shead[N];
int son[S][], pnt[S], val[S], ntot, last;
int head[S], dest[S], next[S], etot;
int dfn[S], dep[S], anc[S][P+], idgr[S], stk[S], qu[S], top, bg, ed, idc;
dnt dp[S], eff[S];
int log[S]; void init() {
ntot = last = ;
pnt[] = -;
}
void append( int c ) {
int p = last;
int np = ++ntot;
val[np] = val[p]+;
while( p!=- && !son[p][c] )
son[p][c]=np, p=pnt[p];
if( p==- ) {
pnt[np] = ;
} else {
int q=son[p][c];
if( val[q]==val[p]+ ) {
pnt[np] = q;
} else {
int nq = ++ntot;
memcpy( son[nq], son[q], sizeof(son[nq]) );
val[nq] = val[p]+;
pnt[nq] = pnt[q];
pnt[q] = pnt[np] = nq;
while( p!=- && son[p][c]==q )
son[p][c]=nq, p=pnt[p];
}
}
last = np;
}
void make_topo() {
for( int u=; u<=ntot; u++ ) {
for( int c=; c<=; c++ ) {
int v=son[u][c];
if( !v ) continue;
idgr[v]++;
}
}
qu[bg=ed=] = ;
while( bg<=ed ) {
int u=qu[bg++];
for( int c=; c<=; c++ ) {
int v=son[u][c];
if( !v ) continue;
idgr[v]--;
if( idgr[v]== )
qu[++ed] = v;
}
}
}
void dodp() {
make_topo();
dp[] = ;
for( int i=; i<=ed; i++ ) {
int u=qu[i];
for( int c=; c<=; c++ ) {
int v=son[u][c];
if( !v ) continue;
dp[v] += dp[u];
}
}
}
void adde( int u, int v ) {
etot++;
dest[etot] = v;
next[etot] = head[u];
head[u] = etot;
}
void build() {
for( int u=; u<=ntot; u++ )
adde( pnt[u], u );
}
void dfs( int u ) {
dfn[u] = ++idc;
for( int p=; p<=P && anc[u][p-]; p++ )
anc[u][p] = anc[anc[u][p-]][p-];
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t];
dep[v] = dep[u]+;
anc[v][] = u;
dfs(v);
}
}
int lca( int u, int v ) {
if( dep[u]<dep[v] ) swap(u,v);
int t=dep[u]-dep[v];
for( int p=; t; p++,t>>= )
if( t& ) u=anc[u][p];
if( u==v ) return u;
for( int p=log[dep[u]]; anc[u][]!=anc[v][]; p-- )
if( anc[u][p]!=anc[v][p] ) u=anc[u][p],v=anc[v][p];
return anc[u][];
}
void fetch( char *s ) {
top = ;
int u = ;
for( int i=; s[i]; i++ ) {
int c=s[i]-'a'+;
u = son[u][c];
assert(u!=);
stk[++top] = u;
}
}
bool cmp( int u, int v ) {
return dfn[u]<dfn[v];
}
void effort( char *s ) {
fetch(s);
sort( stk+, stk++top, cmp );
eff[stk[]] += ;
for( int i=; i<=top; i++ ) {
int u=stk[i];
int ca=lca(u,stk[i-]);
eff[u] += ;
eff[ca] -= ;
}
}
void bfs() {
qu[bg=ed=] = ;
while( bg<=ed ) {
int u=qu[bg++];
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t];
qu[++ed] = v;
}
}
for( int i=ed; i>=; i-- ) {
int u=qu[i];
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t];
eff[u] += eff[v];
}
}
dp[] = ;
for( int i=; i<=ed; i++ ) {
int u=qu[i];
if( eff[u]>=k ) {
dp[u] = dp[pnt[u]]+dp[u];
} else {
dp[u] = dp[pnt[u]];
}
}
}
void query( char *s ) {
fetch(s);
sort( stk+, stk++top, cmp );
dnt rt = ;
for( int i=; i<=top; i++ ) {
int u=stk[i];
rt += dp[u];
}
printf( "%lld ", rt );
}
int main() {
// input and build sam
scanf( "%d%d", &n, &k );
init();
char *buf_cur = buf;
for( int i=; i<=n; i++ ) {
shead[i] = buf_cur;
scanf( "%s", shead[i] );
for( int j=; shead[i][j]; j++ )
append( shead[i][j]-'a'+ );
append( );
buf_cur += strlen(shead[i]) + ;
}
log[] = -;
for( int i=; i<=ntot; i++ ) log[i] = log[i>>]+;
// dodp to calc the number of real substring
dodp();
// build parent tree and its dfs order
build();
dfs();
// calc echo string's effort
for( int i=; i<=n; i++ )
effort( shead[i] );
// bfs to calc the sum of subans
bfs();
// query the answer of eacho string
for( int i=; i<=n; i++ )
query( shead[i] );
printf( "\n" );
}
上一篇:Linux内核设计与实现 总结笔记(第十三章)虚拟文件系统


下一篇:Linux内核设计与实现读书笔记(8)-内核同步方法【转】