您的位置:首页 > 产品设计 > UI/UE

Codeforces Round #146 (Div. 1) C. Cyclical Quest——后缀自动机

2013-08-04 23:56 531 查看
题意:给定一个母串,每次询问一个子串或把子串的前一部分移到末尾形成的串在母串中出现的次数。

处理方法都是很常见的。对询问中的每个子串,设长度为len。复制一份放到其末尾位置并把最后一个字符去掉。再拿这个串和母串的自动机匹配,对于询问串的每一个位置,记录该位置往前能匹配的长度,如果长度大于等于len,则通过pre指针找到从这个位置往前len个字符之间的串在自动机中对应的状态。并将答案加上该状态出现的次数。还要注意,因为复制后的询问串中会有一些长度为len的相同子串出现在不同的位置,那么在匹配的过程中就有可能重复计算,拿我们就在每个状态上加上标记,如果标记过就不再计算。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lng long long
using namespace std;

const int maxn = 3000000 + 10;
int str[maxn], len;
struct suffixautomaton
{
int ch[maxn][30], pre[maxn], val[maxn];
int c[maxn], top[maxn], flag[maxn];
int num[maxn], sz, last;

void init()
{
pre[0] = -1; last = 0; sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
memset(num, 0, sizeof(num));
memset(flag, 0, sizeof(flag));
}

void insert(int x)
{
int p = last, np = sz++;
last = np;
memset(ch[np], 0, sizeof(ch[np]));
val[np] = val[p] + 1;
while(p != -1 && ch[p][x] == 0)
{
ch[p][x] = np;
p = pre[p];
}
if(p == -1) pre[np] = 0;
else
{
int q = ch[p][x];
if(val[q] == val[p] + 1) pre[np] = q;
else
{
int nq = sz++;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
val[nq] = val[p] + 1;
pre[nq] = pre[q];
pre[q] = pre[np] = nq;
while(p != -1 && ch[p][x] == q) { ch[p][x] = nq; p = pre[p]; }
}
}
}

void calc()
{
memset(c, 0, sizeof(c));
for(int i = 0; i < sz; ++i) c[val[i]] += 1;
for(int i = 1; i <= len; ++i) c[i] += c[i - 1];
for(int i = 0; i < sz; ++i) top[--c[val[i]]] = i;
for(int i = 0; ; i = ch[i][str[val[i]]])
{
num[i] = 1;
if(val[i] == len) break;
}
for(int i = sz - 1; i > 0; --i)
num[pre[top[i]]] += num[top[i]];
}

lng query(char * s, int c, int tmplen)
{
lng ans = 0;
int u = 0, l = 0;
for(int i = 0; s[i]; ++i)
{
int x = s[i] - 'a';
if(ch[u][x]) { u = ch[u][x]; l++; }
else
{
while(u != -1 && ch[u][x] == 0) u = pre[u];
if(u == -1) u = 0, l = 0;
else { l = val[u] + 1; u = ch[u][x]; }
}
if(l >= tmplen)
{
int v = u;
while(val[pre[v]] >= tmplen) v = pre[v];
if(flag[v] != c) { flag[v] = c; ans += num[v]; }
}
}
return ans;
}

}sam;

char s[maxn];

int main()
{
scanf("%s", s); len = strlen(s); sam.init();
for(int i = 0; i < len; ++i) { str[i] = s[i] - 'a'; sam.insert(str[i]); }
sam.calc();
int q; scanf("%d", &q);
while(q--)
{
scanf("%s", s);
int tmp = strlen(s);
for(int i = 0; i < tmp - 1; ++i) s[tmp + i] = s[i];
s[2 * tmp - 1] = '\0';
cout << sam.query(s, q + 1, tmp) << "\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: