WA on #1 求助
查看原帖
WA on #1 求助
675466
zzx0102楼主2023/8/12 17:45
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int N = 250010, base = 388651;
string s1, s2; int bit[N], n, m, h1[N], h2[N];
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
unordered_map<ull, bool, custom_hash> mp;
ull get1(int l, int r) {return h1[r] - h1[l - 1] * bit[r - l + 1];}
ull get2(int l, int r) {return h2[r] - h2[l - 1] * bit[r - l + 1];}
bool check(int k) {
	mp.clear();
	for(int i = k; i <= m; i++) mp[get2(i - k + 1, i)] = 1;
	for(int i = k; i <= n; i++) if(mp[get1(i - k + 1, i)]) return 1;
	return 0;
}
int main() {
	cin >> s1 >> s2; n = s1.length(), m = s2.length(); s1 = ' ' + s1; s2 = ' ' + s2;
	bit[0] = 1; for(int i = 1; i <= max(n, m); i++) bit[i] = bit[i - 1] * base;
	for(int i = 1; i <= n; i++) h1[i] = h1[i - 1] * base + s1[i]; for(int i = 1; i <= m; i++) h2[i] = h2[i - 1] * base + s2[i];
	int l = 0, r = min(n, m), ans = -1; while(l <= r) {int mid = l + r >> 1; if(check(mid)) l = mid + 1, ans = mid; else r = mid - 1;} cout << ans;
	return 0;
}
2023/8/12 17:45
加载中...