您的位置:首页 > 其它

【BZOJ】2555: SubString(后缀自动机)

2014-12-04 17:21 591 查看
http://www.lydsy.com/JudgeOnline/problem.php?id=2555

学到了如何快速维护right值orz

(虽然这仍然是暴力维护,但是这是O(n)的暴力233

首先我们在加一个新节点的时候直接遍历它的parent树就行啦啦!!!!这样直接就暴力维护了。。。我竟然没想到。。

然后在复制新节点的时候不要忘记将right也要赋值进去。。

然后就是裸题了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

struct sam {
static const int N=1500004;
int l
, c
[26], f
, root, last, cnt, r
;
sam() { cnt=0; root=last=++cnt; }
void add(int x) {
int now=last, a=++cnt; last=a;
l[a]=l[now]+1;
for(; now && !c[now][x]; now=f[now]) c[now][x]=a;
if(!now) f[a]=root;
else {
int q=c[now][x];
if(l[q]==l[now]+1) f[a]=q;
else {
int b=++cnt;
memcpy(c[b], c[q], sizeof c[q]);
l[b]=l[now]+1;
r[b]=r[q];
f[b]=f[q];
f[q]=f[a]=b;
for(; now && c[now][x]==q; now=f[now]) c[now][x]=b;
}
}
while(a) ++r[a], a=f[a];
}
int find(char *s) {
int now=root, n=strlen(s);
rep(i, n) if(!(now=c[now][s[i]-'A'])) return 0;
return r[now];
}
}a;
void fix(char *s, int last) {
int len=strlen(s);
rep(i, len) {
last=(last*131+i)%len;
swap(s[i], s[last]);
}
}
const int N=3000005;
char s
, w[100];
int main() {
int n=getint(), len, last=0;
scanf("%s", s);
len=strlen(s);
rep(i, len) a.add(s[i]-'A');
for1(i, 1, n) {
scanf("%s%s", w, s);
len=strlen(s);
fix(s, last);
if(w[0]=='Q') { int ans=a.find(s); printf("%d\n", ans); last^=ans; }
else rep(i, len) a.add(s[i]-'A');
}
return 0;
}


  

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 BBABBBBAAB

Sample Output

0

HINT

40 % 的数据字符串最终长度 <= 20000,询问次数<= 1000,询问总长度<= 10000

100 % 的数据字符串最终长度 <= 600000,询问次数<= 10000,询问总长度<= 3000000

Source

Ctsc模拟赛By 洁妹
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: