# bzoj 2555

1、第一道后缀自动机，大概知道怎么写了，有一些原理性的东西还要理解。

2、计算right集合的大小

 1 /**************************************************************
2     Problem: 2555
3     User: idy002
4     Language: C++
5     Result: Accepted
6     Time:4708 ms
7     Memory:143072 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <cstring>
12 #include <iostream>
13 #define fprintf(...)
14 #define N 1200010
15 using namespace std;
16
17 int n, m;
18 char buf[N];
20 int son[N][26], par[N], val[N], siz[N], last, root, ntot;
21
22 int newnode( int v ) {
23     int nd=++ntot;
24     val[nd] = v;
25     return nd;
26 }
27 void init() {
28     root = last = newnode(0);
29     siz[root] = 1;
30 }
31 void append( int c ) {
32     int p=last;
33     int np=newnode( val[p]+1 );
34     for( ; p && !son[p][c]; p=par[p] ) son[p][c] = np;
35     if( p==0 ) {
36         par[np] = root;
37     } else {
38         int q = son[p][c];
39         if( val[q]==val[p]+1 ) {
40             par[np] = q;
41         } else {
42             int nq = newnode( val[p]+1 );
43             memcpy( son[nq], son[q], sizeof(son[nq]) );
44             par[nq] = par[q];
45             siz[nq] = siz[q];
46             par[np] = par[q] = nq;
47             for( ; p && son[p][c]==q; p=par[p] ) son[p][c]=nq;
48         }
49     }
50     last = np;
51     for( ; np; np=par[np] ) ++siz[np];
52 }
53 void append( const char *T ) {
54     fprintf( stderr, "append( %s )\n", T );
55     for( ; *T; T++ )
56         append( *T-'A' );
57 }
58 void decode( int mask ) {
59     fprintf( stderr, "%d\n", mask );
60     int n = strlen(buf);
61     for( int j=0; j<n; j++ ) {
64     }
65 }
66 int query( const char *T ) {
67     fprintf( stderr, "query( %s )\n", T );
68     int n=strlen(T);
69     int u=root;
70     for( int i=0; i<n; i++ ) {
71         int c=T[i]-'A';
72         if( !son[u][c] ) return 0;
73         u=son[u][c];
74     }
75     return siz[u];
76 }
77 int main() {
78     scanf( "%d", &m );
79     scanf( "%s", buf );
80     init();
81     append( buf );
82     for( int i=1; i<=m; i++ ) {
83         char ch[10];
84         scanf( "%s%s", ch, buf );
86         if( ch[0]=='A' ) {
87             append(buf);
88         } else {
89             int ans = query(buf);
90             printf( "%d\n", ans );
94 }