Description
懒得写背景了,给你一个字符串init,要求你支持两个操作
(1):在当前字符串的后面插入一个字符串
(2):询问字符串s在当前字符串中出现了几次?(作为连续子串)
你必须在线支持这些操作。Input
第一行一个数Q表示操作个数
第二行一个字符串表示初始字符串init
接下来Q行,每行2个字符串Type,Str
Type是ADD的话表示在后面插入字符串。
Type是QUERY的话表示询问某字符串在当前字符串中出现了几次。
为了体现在线操作,你需要维护一个变量mask,初始值为0
![]()
读入串Str之后,使用这个过程将之解码成真正询问的串TrueStr。
询问的时候,对TrueStr询问后输出一行答案Result
然后mask = mask xor Result
插入的时候,将TrueStr插到当前字符串后面即可。
HINT:ADD和QUERY操作的字符串都需要解压Output
Sample Input
2
A
QUERY B
ADD BBABBBBAABSample Output
0HINT
40 % 的数据字符串最终长度 <= 20000,询问次数<= 1000,询问总长度<= 10000
100 % 的数据字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000
新加数据一组–2015.05.20
样例差评..(逃)
这道题挺好的啊..
有两种做法:(1)SAM+暴力 (2)SAM+LCT
先说第一种:SAM+暴力
按照SAM建造方法,每新建一个节点就一直跳fail调到0,暴力+1,这样的话可以过大部分点而且比第二种要快..
但是看到没有:新加数据一组–2015.05.20
这组数据就是为第二种做法正名的!!!
暴力做法会TLE
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 600010;
int F[Maxn*2], G[Maxn*2], ch[Maxn*2][26], d[Maxn*2], tot, now, nn;
char s[Maxn]; int len;
int n;
void decode ( int mask ){
int i;
for ( i = 0; i < len; i ++ ){
mask = ( mask*131 + i ) % len;
swap ( s[i], s[mask] );
}
}
int copy ( int p, int c ){
int x = ++tot, y = ch[p][c];
d[x] = d[p]+1; G[x] = G[y];
for ( int i = 0; i < 26; i ++ ) ch[x][i] = ch[y][i];
F[x] = F[y]; F[y] = x;
while ( ~p && ch[p][c] == y ){ ch[p][c] = x; p = F[p]; }
return x;
}
void add ( int c ){
int p, o;
if ( p = ch[now][c] ){
if ( d[p] != d[now]+1 ) copy ( now, c );
now = ch[now][c];
}
else {
d[o=++tot] = d[now]+1; p = now; now = o; G[o] = 1;
while ( ~p && !ch[p][c] ){ ch[p][c] = o; p = F[p]; }
F[o] = ~p ? ( d[p]+1 == d[ch[p][c]] ? ch[p][c] : copy ( p, c ) ) : 0;
while (~F[o]){ G[F[o]] ++; o = F[o]; }
}
}
int main (){
int i, j, k;
scanf ( "%d", &n );
scanf ( "%s", s+1 ); len = strlen (s+1);
F[0] = -1;
for ( i = 1; i <= len; i ++ ) add (s[i]-'A');
int mask = 0;
for ( i = 1; i <= n; i ++ ){
scanf ( "%s", s );
if ( s[0] == 'Q' ){
scanf ( "%s", s );
len = strlen (s);
decode (mask);
bool bk = true;
nn = 0;
for ( j = 0; j < len; j ++ ){
int c = s[j]-'A';
if ( ch[nn][c] == 0 ){ printf ( "0\n" ); bk = false; break; }
nn = ch[nn][c];
}
if ( bk == true ){ printf ( "%d\n", G[nn] ); mask ^= G[nn]; }
}
else {
scanf ( "%s", s );
len = strlen (s);
decode (mask);
for ( j = 0; j < len; j ++ ) add (s[j]-'A');
}
}
return 0;
}
第二种做法:SAM+LCT
首先按照fail可以拉出一棵树,然后fail的改变就相当于加边删边,那就是很裸的LCT模板了..
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 600010;
int F[Maxn*2], d[Maxn*2], ch[Maxn*2][26], tot, now;
int n; char s[Maxn]; int len;
void decode ( int mask ){
for ( int i = 0; i < len; i ++ ){
mask = ( mask*131 + i ) % len;
swap ( s[i], s[mask] );
}
}
int val[Maxn*2], c[Maxn*2][2], fa[Maxn*2], rev[Maxn*2], la[Maxn*2];
bool is_root ( int x ){ return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void push_down ( int x ){
if ( la[x] ){
int lc = c[x][0], rc = c[x][1];
val[lc] += la[x]; val[rc] += la[x];
la[lc] += la[x]; la[rc] += la[x];
la[x] = 0;
}
if ( rev[x] ){
rev[x] = 0;
int lc = c[x][0], rc = c[x][1];
swap ( c[x][0], c[x][1] );
rev[lc] ^= 1; rev[rc] ^= 1;
}
}
void rotate ( int x ){
int y = fa[x], z = fa[y], l, r;
if ( c[y][0] == x ) l = 0; else l = 1; r = l^1;
if ( !is_root (y) ){ if ( c[z][0] == y ) c[z][0] = x; else c[z][1] = x; }
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
}
int st[Maxn*2], tp;
void prep ( int x ){
int i; tp = 0;
for ( i = x; !is_root (i); i = fa[i] ) st[++tp] = i;
st[++tp] = i;
for ( i = tp; i >= 1; i -- ) push_down (st[i]);
}
void splay ( int x ){
prep (x);
while ( !is_root (x) ){
int y = fa[x], z = fa[y];
if ( !is_root (y) ){
if ( (c[y][0]==x)^(c[z][0]==y) ) rotate (x);
else rotate (y);
}
rotate (x);
}
}
void access ( int x ){
int t = 0;
while (x){
splay (x);
c[x][1] = t;
t = x;
x = fa[x];
}
}
void make_root ( int x ){ access (x); splay (x); rev[x] ^= 1; }
void cut ( int x, int y ){ make_root (x); access (y); splay (y); fa[x] = 0; c[y][0] = 0; }
void link ( int x, int y ){ make_root (x); fa[x] = y; }
int query ( int x ){ access (x); splay (x); return val[x]; }
int copy ( int p, int c ){
int x = ++tot, y = ch[p][c];
d[x] = d[p]+1;
for ( int i = 0; i < 26; i ++ ) ch[x][i] = ch[y][i];
cut ( y+1, F[y]+1 ); F[x] = F[y]; link ( x+1, F[x]+1 ); F[y] = x; link ( y+1, F[y]+1 );
val[x+1] = val[y+1];
while ( ~p && ch[p][c] == y ){ ch[p][c] = x; p = F[p]; }
return x;
}
void change ( int x, int y, int c ){ make_root (x); access (y); splay (y); la[y] += c; val[y] += c; }
void add ( int c ){
int p, o;
if ( p = ch[now][c] ){
if ( d[p] != d[now]+1 ) copy ( now, c );
now = ch[now][c];
}
else {
d[o=++tot] = d[now]+1; p = now; now = o; val[o+1] = 1;
while ( ~p && !ch[p][c] ){ ch[p][c] = o; p = F[p]; }
F[o] = ~p ? ( d[p]+1 == d[ch[p][c]] ? ch[p][c] : copy ( p, c ) ) : 0;
link ( o+1, F[o]+1 ); change ( 1, F[o]+1, 1 );
}
}
int main (){
int i, j, k;
scanf ( "%d", &n );
scanf ( "%s", s );
len = strlen (s);
F[0] = -1;
for ( i = 0; i < len; i ++ ) add (s[i]-'A');
char cc[11];
int mask = 0;
for ( i = 1; i <= n; i ++ ){
scanf ( "%s", cc );
scanf ( "%s", s ); len = strlen (s);
decode (mask);
if ( cc[0] == 'Q' ){
int nn = 0;
bool bk = true;
for ( j = 0; j < len; j ++ ){
int c = s[j]-'A';
if ( ch[nn][c] == 0 ){ printf ( "0\n" ); bk = false; break; }
nn = ch[nn][c];
}
if ( bk == true ){
int ans = query (nn+1);
printf ( "%d\n", ans );
mask ^= ans;
}
}
else {
for ( j = 0; j < len; j ++ ) add (s[j]-'A');
}
}
return 0;
}