Description
有一个长度为n的仅包含小写字母的字符串S.现在有若干组询问,对于每一个询问给出若干个后缀,求这些后缀两两之间的LCP的长度之和.
Solution
先构建出后缀数组,对于每个询问,由于计算的是相邻元素的height
,所以我们按照rank
排序。(记得去重!!!)然后分别从前、后扫一遍,求出每个元素向左、向右第一个height
比它小的元素的位置(用单调栈维护
O(n)
O
(
n
)
),然后直接计算即可。
Code
/************************************************
* Au: Hany01
* Date: Apr 8th, 2018
* Prob: [BZOJ3879] SvT
* 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 (23333333333333333ll)
#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 = 1000005;
int c[maxn], n, m, tot, h[maxn], hh[maxn], f[maxn][20], sa[maxn], rk[maxn], tp[maxn], Log[maxn], height[maxn], stk[maxn], top, L[maxn], R[maxn];
char s[maxn];
LL Ans;
inline void RadixSort()
{
For(i, 0, m) c[i] = 0;
For(i, 1, n) ++ c[rk[i]];
For(i, 1, m) c[i] += c[i - 1];
Fordown(i, n, 1) sa[c[rk[tp[i]]] --] = tp[i];
}
inline void getSA()
{
For(i, 1, n) rk[i] = s[i], tp[i] = i;
m = 255, RadixSort();
for (int k = 1, p; k <= n; k <<= 1) {
p = 0;
For(i, n - k + 1, n) tp[++ p] = i;
For(i, 1, n) if (sa[i] > k) tp[++ p] = sa[i] - k;
RadixSort(), swap(rk, tp), rk[sa[1]] = 1, m = 1;
For(i, 2, n) rk[sa[i]] = tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + k] == tp[sa[i - 1] + k] ? m : ++ m;
if (m >= n) return ;
}
}
inline void getHeight()
{
for (int i = 1, k = 0, j; i <= n; height[rk[i ++]] = k)
for (k = k ? k - 1 : k, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++ k) ;
}
inline void initST()
{
For(i, 2, n) Log[i] = Log[i >> 1] + 1;
For(i, 2, n) f[i][0] = height[i];
For(j, 1, Log[n])
For(i, 2, n) f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
inline int getmin(int l, int r) {
int ld = Log[r - l + 1];
return min(f[l][ld], f[r - (1 << ld) + 1][ld]);
}
inline bool cmp(const int& x, const int& y) { return rk[x] < rk[y]; }
int main()
{
#ifdef hany01
File("bzoj3879");
#endif
static int q;
n = read(), q = read();
scanf("%s", s + 1);
getSA();
getHeight();
initST();
while (q --)
{
tot = read() - 1;
For(i, 1, tot + 1) hh[i] = read();
sort(hh + 1, hh + 1 + tot + 1);
tot = unique(hh + 1, hh + 1 + tot + 1) - hh - 2;
sort(hh + 1, hh + 2 + tot, cmp);
For(i, 1, tot) h[i] = getmin(rk[hh[i]] + 1, rk[hh[i + 1]]);
h[0] = h[tot + 1] = -INF;
stk[top = 1] = 0;
For(i, 1, tot) {
while (top && h[stk[top]] >= h[i]) -- top;
L[i] = stk[top] + 1, stk[++ top] = i;
}
stk[top = 1] = tot + 1;
Fordown(i, tot, 1) {
while (top && h[stk[top]] > h[i]) -- top;
R[i] = stk[top] - 1, stk[++ top] = i;
}
Ans = 0;
For(i, 1, tot) (Ans += ((LL)(R[i] - i + 1) * (LL)(i - L[i] + 1) % Mod * (LL)h[i]) % Mod) %= Mod;
printf("%lld\n", Ans);
}
return 0;
}
//君若清路尘,妾若浊水泥;
// -- 曹植《明月上高楼》