本文主要是介绍【HDU】5566 Clarke and room【树链剖分+AC自动机】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:Clarke and room
#include <bits/stdc++.h>
using namespace std ;typedef long long LL ;#define ls ( o << 1 )
#define lson ls , l , m
#define rs ( o << 1 | 1 )
#define rson rs , m + 1 , r
#define rt 1 , 1 , tree_idx
#define mid ( ( l + r ) >> 1 )
#define clr( a , x ) memset ( a , x , sizeof a )const int MAXN = 100005 ;
const int MAXE = 200005 ;struct Edge {int v , n ;Edge () {}Edge ( int v , int n ) : v ( v ) , n ( n ) {}
} ;struct Point {int len , idx ;Point () {}Point ( int len , int idx ) : len ( len ) , idx ( idx ) {}
} ;struct Query {int x , y , idx ;Query () {}Query ( int x , int y , int idx ) : x ( x ) , y ( y ) , idx ( idx ) {}
} ;struct Node {Node* nxt[26] ;Node* fail ;vector < Point > S ;vector < Query > Q ;vector < Node* > N ;
} ;Node node[MAXN] ;
int head , tail ;
Node* Q[MAXN] ;
Node* root ;
Node* cur ;Edge E[MAXE] ;
int H[MAXN] , cntE ;
int maxv[MAXN << 2] ;
int dep[MAXN] ;
int top[MAXN] ;
int pos[MAXN] ;
int pre[MAXN] ;
int siz[MAXN] ;
int son[MAXN] ;
int tree_idx ;int ans[MAXN] ;
char s[MAXN] ;
int n , m ;void addedge ( int u , int v ) {E[cntE] = Edge ( v , H[u] ) ;H[u] = cntE ++ ;
}void dfs ( int u , int f ) {siz[u] = 1 ;son[u] = 0 ;for ( int i = H[u] ; ~i ; i = E[i].n ) {int v = E[i].v ;if ( v == f ) continue ;dep[v] = dep[u] + 1 ;pre[v] = u ;dfs ( v , u ) ;siz[u] += siz[v] ;if ( siz[son[u]] < siz[v] ) son[u] = v ;}
}void rebuild ( int u , int top_element ) {top[u] = top_element ;pos[u] = ++ tree_idx ;if ( son[u] ) rebuild ( son[u] , top_element ) ;for ( int i = H[u] ; ~i ; i = E[i].n ) {int v = E[i].v ;if ( v != son[u] && v != pre[u] ) rebuild ( v , v ) ;}
}void update ( int x , int v , int o , int l , int r ) {if ( l == r ) {maxv[o] = v ;return ;}int m = mid ;if ( x <= m ) update ( x , v , lson ) ;else update ( x , v , rson ) ;maxv[o] = max ( maxv[ls] , maxv[rs] ) ;
}int query ( int L , int R , int o , int l , int r ) {if ( L <= l && r <= R ) return maxv[o] ;int m = mid , ans = 0 ;if ( L <= m ) ans = max ( query ( L , R , lson ) , ans ) ;if ( m < R ) ans = max ( query ( L , R , rson ) , ans ) ;return ans ;
}int ask ( int x , int y , int ans = 0 ) {while ( top[x] != top[y] ) {if ( dep[top[x]] < dep[top[y]] ) swap ( x , y ) ;ans = max ( ans , query ( pos[top[x]] , pos[x] , rt ) ) ;x = pre[top[x]] ;}if ( dep[x] > dep[y] ) swap ( x , y ) ;ans = max ( ans , query ( pos[x] , pos[y] , rt ) ) ;return ans ;
}Node* newnode () {clr ( cur->nxt , 0 ) ;cur->fail = NULL ;cur->S.clear () ;cur->Q.clear () ;cur->N.clear () ;return cur ++ ;
}void init () {cntE = 0 ;cur = node ;tree_idx = 0 ;clr ( H , -1 ) ;clr ( maxv , 0 ) ;root = newnode () ;
}void insert ( char s[] , int len , int idx ) {Node* now = root ;for ( int i = 0 ; i < len ; ++ i ) {int x = s[i] - 'a' ;if ( !now->nxt[x] ) now->nxt[x] = newnode () ;now = now->nxt[x] ;}now->S.push_back ( Point ( len , idx ) ) ;
}void build () {root->fail = root ;head = tail = 0 ;for ( int i = 0 ; i < 26 ; ++ i ) {if ( !root->nxt[i] ) root->nxt[i] = root ;else {root->nxt[i]->fail = root ;Q[tail ++] = root->nxt[i] ;root->N.push_back ( root->nxt[i] ) ;}}while ( head != tail ) {Node* now = Q[head ++] ;for ( int i = 0 ; i < 26 ; ++ i ) {if ( !now->nxt[i] ) now->nxt[i] = now->fail->nxt[i] ;else {now->nxt[i]->fail = now->fail->nxt[i] ;Q[tail ++] = now->nxt[i] ;now->fail->nxt[i]->N.push_back ( now->nxt[i] ) ;}}}
}void insert_query ( int x , int y , char s[] , int idx ) {Node* now = root ;for ( int i = 0 ; s[i] ; ++ i ) {now = now->nxt[s[i] - 'a'] ;now->Q.push_back ( Query ( x , y , idx ) ) ;}
}void dfs2 ( Node* now ) {for ( int i = 0 ; i < now->S.size () ; ++ i ) {update ( pos[now->S[i].idx] , now->S[i].len , rt ) ;}for ( int i = 0 ; i < now->Q.size () ; ++ i ) {int idx = now->Q[i].idx ;ans[idx] = max ( ans[idx] , ask ( now->Q[i].x , now->Q[i].y ) ) ;}for ( int i = 0 ; i < now->N.size () ; ++ i ) {dfs2 ( now->N[i] ) ;}for ( int i = 0 ; i < now->S.size () ; ++ i ) {update ( pos[now->S[i].idx] , 0 , rt ) ;}
}void solve () {int x , y ;init () ;scanf ( "%d" , &n ) ;for ( int i = 1 ; i <= n ; ++ i ) {scanf ( "%s" , s ) ;int l = strlen ( s ) ;insert ( s , l , i ) ;}build () ;for ( int i = 2 ; i <= n ; ++ i ) {scanf ( "%d" , &x ) ;addedge ( x , i ) ;addedge ( i , x ) ;}dfs ( 1 , 0 ) ;rebuild ( 1 , 1 ) ;scanf ( "%d" , &m ) ;for ( int i = 0 ; i < m ; ++ i ) {scanf ( "%d%d%s" , &x , &y , s ) ;insert_query ( x , y , s , i ) ;ans[i] = 0 ;}dfs2 ( root ) ;for ( int i = 0 ; i < m ; ++ i ) {printf ( "%d\n" , ans[i] ) ;}
}int main () {int T ;scanf ( "%d" , &T ) ;for ( int i = 1 ; i <= T ; ++ i ) {solve () ;}return 0 ;
}
这篇关于【HDU】5566 Clarke and room【树链剖分+AC自动机】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!