# BZOJ3473: 字符串【后缀数组+思维】

3 1
abc
a
ab

6 1 3

## 思路

 #include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;
const int N = 2e5 + 10;
const int LOG = 20;

struct Suffix_Array {
int s[N], n, m;
int c[N], x[N], y[N];
int height[N], sa[N], rank[N];
int st[N][LOG], Log[N];
ll sum[N];

void init(int len, char *c) {
n = len, m = 0;
for (int i = 1; i <= len; i++) {
s[i] = c[i];
m = max(m, s[i]);
}
}

for (int i = 1; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) c[x[y[i]]]++;
for (int i = 1; i <= m; i++) c[i] += c[i - 1];
for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
}

void buildsa() {
for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
int now;
for (int k = 1; k <= n; k <<= 1) {
now = 0;
for (int i = n - k + 1; i <= n; i++) y[++now] = i;
for (int i = 1; i <= n; i++) if (sa[i] > k) y[++now] = sa[i] - k;
y[sa[1]] = now = 1;
for (int i = 2; i <= n; i++) y[sa[i]] = (x[sa[i]] == x[sa[i - 1]] && x[sa[i] + k] == x[sa[i - 1] + k]) ? now : ++now;
swap(x, y);
if (now == n) break;
m = now;
}
}

void buildrank() {
for (int i = 1; i <= n; i++) rank[sa[i]] = i;
}

void buildsum() {
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + n - sa[i] + 1 - height[i];
}

void buildheight() {
for (int i = 1; i <= n; i++) if (rank[i] != 1) {
int k = max(height[rank[i - 1]] - 1, 0);
for (; s[i + k] == s[sa[rank[i] - 1] + k]; k++);
height[rank[i]] = k;
}
}

void buildst() {
Log[1] = 0;
for (int i = 2; i < N; i++) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= n; i++) st[i][0] = height[i];
for (int j = 1; j < LOG; j++) {
for (int i = 1; i + (1 << (j - 1)) <= n; i++) {
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}

int queryst(int l, int r) {
if (l == r) return n - sa[l] + 1;
if (l > r) swap(l, r);
++l; //***
int k = Log[r - l + 1];
return min(st[l][k], st[r - (1 << k) + 1][k]);
}

int querylcp(int la, int lb) {
return queryst(rank[la], rank[lb]);
}

int querylcp(int la, int ra, int lb, int rb) {
return min(min(ra - la + 1, rb - lb + 1), queryst(rank[la], rank[lb]));
}

void build(int len, char *c) {
init(len, c);
buildsa();
buildrank();
buildheight();
buildsum();
buildst();
}
} Sa;

char s[N], c[N];
int len[N], bg[N], ed[N], tot = 0;
int n, k, rpos[N], num[N], bel[N];

bool check(int pos, int len) {
int x = Sa.rank[pos], l, r;
int l_line = x, r_line = x;
l = 1, r = x - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (Sa.queryst(x, mid) > len) l_line = mid, r = mid - 1;
else l = mid + 1;
}
l = x + 1, r = tot;
while (l <= r) {
int mid = (l + r) >> 1;
if (Sa.queryst(x, mid) > len) r_line = mid, l = mid + 1;
else r = mid - 1;
}
return rpos[l_line] <= r_line;
}

int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%s", c + 1);
len[i] = strlen(c + 1);
bg[i] = tot + 1;
for (int j = 1; j <= len[i]; j++) {
s[++tot] = c[j];
bel[tot] = i;
}
ed[i] = tot;
s[++tot] = '#';
}
Sa.build(tot, s);
memset(rpos, 0x3f, sizeof(rpos)); //*****
int l = 1, r = 0, cnt = 0;
for (; r <= tot; r++) {
if (!bel[Sa.sa[r]]) continue;
++num[bel[Sa.sa[r]]];
if (num[bel[Sa.sa[r]]] == 1) ++cnt;
if (cnt >= k) {
for (; l <= r; l++) {
if (!bel[Sa.sa[l]]) continue;
if (cnt >= k) rpos[l] = r;
else break;
if (num[bel[Sa.sa[l]]] == 1) --cnt;
--num[bel[Sa.sa[l]]];
}
}
}
for (int i = 1; i <= n; i++) {
ll ans = 0; int cur = 0;
for (int j = bg[i]; j <= ed[i]; j++) {
cur = max(cur - 1, 0);
while (j + cur <= ed[i] && check(j, cur)) ++cur;
ans += cur;
}
printf("%lld ", ans);
}
return 0;
}
posted @ 2018-12-05 22:06  Dream_maker_yk  阅读(...)  评论(...编辑  收藏