Description
给定一个长度为 n n 的串( n≤2000 n ≤ 2000 ),给出 m m 次询问,每次询问串有多少个不同的子串。
Solution
SAM水题。
发现串比较短,直接枚举开头位置依次插入SAM,预处理出 ansi,j a n s i , j 表示 S(i,j) S ( i , j ) 的答案即可。
Code
/************************************************
* Au: Hany01
* Date: May 10th, 2018
* Prob: [HDU4622] Reincarnation
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 2005;
char s[maxn];
int n, ch[maxn << 1][26], fa[maxn << 1], len[maxn << 1], sum, ans[maxn][maxn], las, tot;
inline void extend(int c)
{
int np = ++ tot, p = las;
las = np, len[np] = len[p] + 1;
while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
if (!p) fa[np] = 1;
else {
int q = ch[p][c];
if (len[q] == len[p] + 1) fa[np] = q;
else {
int nq = ++ tot;
Cpy(ch[nq], ch[q]), fa[nq] = fa[q], len[nq] = len[p] + 1, fa[np] = fa[q] = nq;
while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
sum += len[np] - len[fa[np]];
}
int main()
{
#ifdef hany01
File("hdu4622");
#endif
for (static int T = read(), m, l, r; T --; )
{
scanf("%s", s + 1), n = strlen(s + 1);
For(i, 1, n) {
Set(ch, 0), tot = las = 1, sum = 0;
For(j, i, n) extend(s[j] - 97), ans[i][j] = sum;
}
for (m = read(); m --; )
l = read(), r = read(), printf("%d\n", ans[l][r]);
}
return 0;
}
//为问山翁何事,坐看流年轻度,拚却鬓双华。
// -- 叶梦得《水调歌头·秋色渐将晚》