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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 1300006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; char ch[MAXN];
namespace LCT { int ch[MAXN][2] , fa[MAXN] , S[MAXN] , lz[MAXN]; inline bool notroot( int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; } inline void add( int x , int c ) { if( x ) lz[x] += c , S[x] += c; } inline void pd( int x ) { if( lz[x] ) { if( ch[x][0] ) add( ch[x][0] , lz[x] ); if( ch[x][1] ) add( ch[x][1] , lz[x] ); lz[x] = 0; } } inline void rotate( int x ) { int f = fa[x] , g = fa[f] , w = ch[fa[x]][1] == x; int wf = ch[g][1]==f , k = ch[x][w^1]; if( notroot(f) ) ch[g][wf] = x; ch[f][w] = k , ch[x][w^1] = f; fa[f] = x , fa[k] = f , fa[x] = g; } void update( int x ) { if( notroot( x ) ) update( fa[x] ); pd( x ); } void splay( int x ) { update( x ); int f , g; while( notroot( x ) ) { f = fa[x] , g = fa[f]; if( notroot( f ) ) rotate( (ch[f][0]==x)^(ch[g][0]==f) ? x : f ); rotate( x ); } } void ac( int x ) { for( int p = 0 ; x ; ( p = x , x = fa[x] ) ) splay( x ) , ch[x][1] = p; } void link( int u , int v ) { fa[u] = v; ac( v ) , splay( v ); add( v , S[u] ); } void cut( int u ) { ac( u ) , splay( u ); add( ch[u][0] , -S[u] ); fa[ch[u][0]] = 0; ch[u][0] = 0; } }
struct SAM { int son[MAXN][26] , par[MAXN] , len[MAXN]; int head[MAXN] , to[MAXN] , nex[MAXN] , ecn; int num[MAXN]; int lst , cnt; void init( ) { lst = cnt = 1 , ecn = 0; } void ade( int u , int v ) { to[++ ecn] = v , nex[ecn] = head[u] , head[u] = ecn; } void addall( ) { for( int i = 1 ; i <= cnt ; ++ i ) ade( par[i] , i ); } void ins( int x ) { using namespace LCT; int cur = ++ cnt; len[cur] = len[lst] + 1 , num[cur] = 1; S[cur] = 1; int p = lst; while( p && !son[p][x] ) son[p][x] = cur , p = par[p]; if( !p ) par[cur] = 1 , link( cur , 1 ); else { int q = son[p][x]; if( len[q] == len[p] + 1 ) par[cur] = q , link( cur , q ); else { int ct = ++ cnt; len[ct] = len[p] + 1 , par[ct] = par[q] , link( ct , par[q] ); cut( q ); memcpy( son[ct] , son[q] , sizeof son[q] ); par[q] = par[cur] = ct , link( q , ct ) , link( cur , ct ); for( ; son[p][x] == q ; p = par[p] ) son[p][x] = ct; } } lst = cur; } int wkr( char* ch , int u ) { if( !u ) return 0; if( ch[0] != 0 ) return wkr( ch + 1 , son[u][ch[0] - 'A'] ); else { using namespace LCT; splay( u ); return S[u]; } } } S ;
void decodeWithMask(char* s, int len , int mask) { for (int j = 0; j < len; j++ ) { mask = (mask * 131 + j) % len; char t = s[j]; s[j] = s[mask]; s[mask] = t; } }
void solve() { cin >> m; scanf("%s",ch + 1); n = strlen( ch + 1 ); S.init(); rep( i , 1 , n ) S.ins( ch[i] - 'A' ); char op[10]; int mask = 0 , ans = 0; while( m-- ) { scanf("%s",op); if( op[0] == 'A' ) { scanf("%s",ch + 1); n = strlen( ch + 1 ); decodeWithMask( ch + 1 , n , mask ); rep( i , 1 , n ) S.ins( ch[i] - 'A' ); } else { scanf("%s",ch + 1); n = strlen( ch + 1 ); decodeWithMask( ch + 1 , n , mask ); printf("%d\n",ans = S.wkr( ch + 1 , 1 ) ); mask ^= ans; } } }
signed main() {
solve(); }
|