Description
Given you n strings, ask how many substrings (excluding empty strings) of each string are substrings (including itself) of at least k of all n strings.
Solution
Build a generalized suffix automaton to find out how many substrings each node is, denoted as size[u]
.
For each string, enumerate the right endpoints and find the satisfying size[u]>k
point, len[u]
that is, its contribution.
Time complexity is still a mystery. . .
Code
/************************************************
* Au: Hany01
* Date: May 6th, 2018
* Prob: [BZOJ3277] 串
* Email: [email protected]
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef set<int>::iterator It;
#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 = 100005;
int n, k, len[maxn << 1], sz[maxn << 1], ch[maxn << 1][26], fa[maxn << 1], v[maxn << 1], nex[maxn << 1], beg[maxn << 1], e, L[maxn], tot = 1, las;
vector<int> s[maxn];
set<int> Set[maxn << 1];
char str[maxn];
inline void extend(int c, int id)
{
int np = ++ tot, p = las;
las = tot, len[np] = len[p] + 1, Set[np].insert(id);
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;
fa[nq] = fa[q], Cpy(ch[nq], ch[q]), len[nq] = len[p] + 1;
fa[np] = fa[q] = nq;
while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
}
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void dfs(int u)
{
for (register int i = beg[u]; i; i = nex[i]) {
dfs(v[i]);
while (Set[v[i]].begin() != Set[v[i]].end())
Set[u].insert(*Set[v[i]].begin()), Set[v[i]].erase(Set[v[i]].begin());
}
sz[u] = SZ(Set[u]);
}
int main()
{
#ifdef hany01
File("bzoj3277");
#endif
n = read(), k = read();
//Assert
if (n < k) {
rep(i, n) printf("0 ");
return 0;
}
//Init
For(i, 1, n) {
scanf("%s", str), las = 1;
rep(j, L[i] = strlen(str)) s[i].pb(str[j] - 97), extend(s[i][j], i);
}
//Build Tree
For(i, 2, tot) add(fa[i], i);
dfs(1);
//Count
For(i, 1, n) {
LL Ans = 0; int u = 1;
rep(j, L[i]) {
u = ch[u][s[i][j]];
while (sz[u] < k) u = fa[u];
Ans += len[u];
}
printf("%lld ", Ans);
}
return 0;
}
//《马嵬坡》
//郑畋
//玄宗回马杨妃死,云雨难忘日月新。
//终是圣明天子事,景阳宫井又何人。