(http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105904#problem/B)
题意:串s,|s|<=2000,Q组查询,查询s(l,r)中有多少不想同子串。
解法:O(n^2)做法。分别从0~len-1为起点作后缀自动机。因为后缀自动机是支持 增 量的。每次加入一个点np,step[np] - step[pre[np]]即为增加的字符串数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)
const int maxn = 2000*4+100;
struct suffix_automaton{
int son[maxn][26],pre[maxn],step[maxn];
int last,si;
void pushback(int v)
{
step[++si]=v;
pre[si]=-1;
clr(son[si],-1);
}
int Extend(int ch)
{
pushback(step[last]+1);
int p=last,np=si;
while(p!=-1&&son[p][ch]==-1)
son[p][ch]=np,
p=pre[p];
if(p==-1) pre[np]=0;
else{
int q=son[p][ch];
if( step[q]==step[p]+1 ) pre[np]=q;
else{
pushback(step[p]+1);
int nq=si;
memcpy(son[nq],son[q],sizeof son[q]);
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
while(p!=-1&&son[p][ch]==q)
son[p][ch]=nq,
p=pre[p];
}
}
last=np;
if(pre[np]==-1) return step[np];
else return step[np]-step[pre[np]];
}
void init()
{
si=last=0;
clr(son[0],-1);
pre[0]=-1;
step[0]=0;
}
}su;
char s[maxn];
int ans[2000+30][2000+30];
int main()
{
//freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--){
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++){
su.init();
int t=0;
for(int j=i;j<len;j++)
{
t += su.Extend(s[j]-'a');
ans[i][j]=t;
}
}
int q,l,r;
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
printf("%d\n",ans[l-1][r-1]);
}
}
return 0;
}