BZOJ4698: Sdoi2008 Sandy的卡片(二分 hash)
题意
Sol
用什么后缀数组啊
直接差分之后 二分+hash找最长公共子串就赢了啊。。。
时间复杂度:\(O(nlogn)\)(不过我写的是两个log。。反正也能过)
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int MAXN = 1e6 + 10;
const ull base = 27;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, mx;
ull po[MAXN];
vector<ull> ha[MAXN];
vector<int> v[MAXN];
map<ull, int> mp, vis;
ull getha(int id, int l, int r) {
if(l == 0) return ha[id][r];
else return ha[id][r] - ha[id][l - 1] * po[r - l + 1];
}
int check(int len) {
mp.clear();
for(int i = 1; i <= N; i++) {
vis.clear();
for(int j = 0; j < v[i].size() - len + 1; j++) {
ull tmp = getha(i, j, j + len - 1);
if(!vis[tmp]) vis[tmp] = 1, mp[tmp]++;
if(mp[tmp] == N) return 1;
}
}
return 0;
}
int main() {
N = read();
po[0] = 1;
for(int i = 1; i <= 10001; i++) po[i] = base * po[i - 1];
for(int i = 1; i <= N; i++) {
int num = read(); mx = max(mx, num);
for(int j = 1; j <= num; j++) v[i].push_back(read());
for(int j = num - 1; j >= 1; j--) v[i][j] = v[i][j] - v[i][j - 1];
ha[i].reserve(v[i].size() + 1);
}
for(int i = 1; i <= N; i++) {
for(int j = 0; j < v[i].size(); j++) {
if(!j) ha[i][j] = 0;
else ha[i][j] = (ha[i][j - 1] + v[i][j]) * base;
}
}
int l = 1, r = mx, ans = 0;
while(l <= r) {
int mid = l + r >> 1;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
cout << ans + 1;
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。