Description
给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数。两个方案不同当且仅当这两个子串中有一个位置不同。
Solution
考虑建出一个串的SAM,让另一个串在上面跑。
记贡献的时候,当前节点的贡献为(len−longest[fa[u]])×sz[u](len−longest[fa[u]])×sz[u](lenlen为当前匹配到的点之前可以被匹配的最大长度)加上所有祖先节点的贡献
Code
/************************************************
* Au: Hany01
* Date: May 25th, 2018
* Prob: [BZOJ4566][LOJ2064][HAOI2016] 找相同字符
* 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 = 200005;
char s1[maxn], s2[maxn];
int n1, n2;
int tot = 1, las = 1, bkt[maxn], per[maxn << 1], len[maxn << 1], fa[maxn << 1], ch[maxn << 1][26];
LL fasz[maxn << 1], sz[maxn << 1];
inline void extend(int c)
{
int np = ++ tot, p = las;
las = np, len[np] = len[p] + 1, sz[np] = 1;
while (!ch[p][c] && p) 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];
}
}
}
int main()
{
#ifdef hany01
File("bzoj4566");
#endif
scanf("%s%s", s1, s2), n1 = strlen(s1), n2 = strlen(s2);
rep(i, n1) extend(s1[i] - 97);
For(i, 1, tot) ++ bkt[len[i]];
For(i, 1, n1) bkt[i] += bkt[i - 1];
For(i, 1, tot) per[bkt[len[i]] --] = i;
Fordown(i, tot, 1) sz[fa[per[i]]] += sz[per[i]];
For(i, 1, tot)
fasz[per[i]] = fasz[fa[per[i]]] + sz[per[i]] * 1ll * (len[per[i]] - len[fa[per[i]]]);
int u = 1, beg = 0;
LL Ans = 0;
rep(i, n2)
{
int c = s2[i] - 97;
if (ch[u][c]) u = ch[u][c];
else {
while (u && !ch[u][c]) u = fa[u];
if (!u) u = 1, beg = i + 1; else chkmax(beg, i - len[u]), u = ch[u][c];
}
if (u > 1) Ans += fasz[fa[u]] + (i - beg + 1 - len[fa[u]]) * 1ll * sz[u];
}
printf("%lld\n", Ans);
return 0;
}
//相思谩然自苦,算云烟、过眼总成空。
// -- 戴复古《木兰花慢·莺啼啼不尽》