做法:
- SA做法: 复杂度O(t * q * (n*log(n))) 暴力解法,注:(q>n) 7e8 险过……
对于每次查询,把这段区间内的字符串,建立后缀数组,然后用(n-1)-sa[i]-height[i]计算。 - SAM做法: 复杂度O(n^2+q) 4e6
在线建立串S每个后缀的SAM,用一个变量维护区间内子串的个数,保存到二维数组里面。离线查询即可
后缀数组:
AC代码: 2449ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end()
#define mk(x,y) make_pair(x,y)
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e5+5;
const int maxn =1e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
const int m = 128; //
char s[maxn]; //待排序的字符串放在s数组中,从s[0~n-1],长度为n,且最大值小于m,最后一位是0(无效值)
int sa[maxn],t1[maxn],t2[maxn],c[maxn],n; //sa[1~n]为有效值,sa[0]必定为n是无效值
int rk[maxn]; //rank[0~n-1]为有效值,rank[n]必定为0无效值
int height[maxn]; //height[1~n]
//lcp(x,y):字符串x与字符串y的最长公共前缀,在这里指x号后缀与与y号后缀的最长公共前缀
//height[i]:lcp(sa[i],sa[i-1]),即排名为i的后缀与排名为i-1的后缀的最长公共前缀
//H[i]:height[rak[i]],即i号后缀与它前一名的后缀的最长公共前缀
inline int read()
{
char x;
int u,flag = 0;
while(x = getchar(),x<'0' || x>'9') if(x == '-') flag = 1;
u = x-'0';
while(x = getchar(),x>='0' && x<='9') u = (u<<3)+(u<<1)+x-'0';
if(flag) u = -u;
return u;
}
void build_sa(int m)
{
n++;
int *x = t1,*y = t2;
for(int i=0;i<m;i++) c[i] = 0;
for(int i=0;i<n;i++) c[x[i] = s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]] = i;
for(int j=1;j<=n;j<<=1)
{
int p = 0;
for(int i = n-j;i<n;i++) y[p++] = i;
for(int i=0;i<n;i++) if(sa[i]>=j) y[p++] = sa[i]-j;
for(int i=0;i<m;i++) c[i] = 0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1;x[sa[0]] = 0;
for(int i=1;i<n;i++)
x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+j] == y[sa[i]+j])?p-1:p++;
if(p>=n) break;
m = p;
}
n--;
int k = 0;
for(int i=0;i<=n;i++) rk[sa[i]] = i;
for(int i=0;i<n;i++){
if(k) k--;
int j = sa[rk[i]-1];
while(s[i+k] == s[j+k]) k++;
height[rk[i]] = k;
}
}
int main()
{
// fin;
// IO;
int t,q;
t = read();
while(t--)
{
string tmp;
scanf("%s",s);
tmp = s;
q = read();
for(int i=1;i<=q;i++)
{
int l,r;
l = read();
r = read();
strcpy(s,tmp.substr(l-1,(r-l+1)).c_str());
n = r-l+1;
build_sa(m);
int ans = 0;
for(int i=1;i<=n;i++) ans += n-sa[i]-height[i];
printf("%d\n",ans);
}
}
return 0;
}
后缀自动机:
AC代码: 748ms
#include<bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end(
#define mk(x,y) make_pair(x,y)
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 2e3+5;
const int INF = 0x3f3f3f3f;
int ans[maxn][maxn];
inline int read()
{
char x;
int u,flag = 0;
while(x = getchar(),x<'0'|| x>'9') if(x == '-') flag = 1;
u = x-'0';
while(x = getchar(),x>='0' && x<='9') u = (u<<3)+(u<<1)+x-'0';
if(flag) u = -u;
return u;
}
char s[maxn];
struct SuffixAutoMation{
int last,cnt,c[maxn<<1],rk[maxn<<1],tot;
int trans[maxn<<1][26],slink[maxn<<1],l[maxn<<1];
inline void init()
{
tot = 0;
last = cnt = 1;
memset(trans,0,sizeof(trans));
slink[last] = l[last] = 0;
}
inline int add(int x)
{
int p = last,np = ++cnt;last = np;l[np] = l[p]+1;
for(;p&&!trans[p][x];p=slink[p]) trans[p][x] = np;
if(!p) slink[np] = 1;
else{
int q = trans[p][x];
if(l[p]+1 == l[q]) slink[np] = q;
else{
int nq = ++cnt;l[nq] = l[p]+1;
memcpy(trans[nq],trans[q],sizeof(trans[q]));
slink[nq] = slink[q];
slink[q] = slink[np] = nq;
for(;trans[p][x] == q;p = slink[p]) trans[p][x] = nq;
}
}
tot+=l[np]-l[slink[np]];
return tot;
}
}sam;
int main()
{
// fin;
// IO;
int t;
t = read();
while(t--)
{
scanf("%s",s+1);
int len = strlen(s+1);
for(int i=1;i<=len;i++)
{
sam.init();
for(int j=i;j<=len;j++)
ans[i][j] = sam.add(s[j]-'a');
}
int q; q = read();
while(q--)
{
int l,r;
l = read(); r = read();
printf("%d\n",ans[l][r]);
}
}
return 0;
}