三个不同,三个叶子,分治解决。
时间复杂度O(n*log m)
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#ifdef LOCAL
#define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n"
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#else
#define TIME 0
#endif
#define hash_ 1000000009
#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
inline int read() { static char buf[1000000], *p1 = buf, *p2 = buf; int x = false; char ch = gc; bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }
template<class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); };
ll fpow(ll a, ll b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10;
char s[N];
char t[N];
int off;
struct StringHash
{
ull h[N], w[N], base;
void Init(ll b, ll m)
{
h[0] = 0, w[0] = 1, base = b;
}
void Add(char* s)
{
int len = strlen(s + 1);
for (int i = 1; i <= len; i++)
h[i] = h[i - 1] * base + s[i], w[i] = w[i - 1] * base;
}
ull Get(int l, int r)
{
if (l > r)
return 0;
return h[r] - h[l - 1] * w[r - l + 1];
}
}ha1, ha2;
int cnt;
void solve(int L, int R)
{
if (cnt > 4)
return;
if (L == R)
{
cnt++;
return;
}
int mid = (L + R >> 1);
if (ha1.Get(L + off, mid + off) != ha2.Get(L, mid))
solve(L, mid);
if (ha1.Get(mid + 1 + off, R + off) != ha2.Get(mid + 1, R))
solve(mid + 1, R);
}
int main()
{
#ifdef LOCAL
freopen("E:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
ha1.Init(233, 1e9 + 7);
ha2.Init(233, 1e9 + 7);
while (T--)
{
scanf("%s", s + 1);
scanf("%s", t + 1);
int n = strlen(s + 1), m = strlen(t + 1);
ha1.Add(s), ha2.Add(t);
int ans = 0;
for (int i = 1; i <= n - m + 1; i++)
{
cnt = 0;
off = i - 1;
solve(1, m);
ans += cnt < 4;
}
cout << ans << endl;
}
return TIME;
}
京公网安备 11010502036488号