zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    CentOS 8上安装MySQL 8.0
    centos8安装redis
    virtualbox虚拟机ubuntu18.04网络配置--虚拟机和宿主机互通且联网
    Controller同时接收MultipartFile数据和json数据
    日期转换
    Python的Mysql操作
    Go语言学习笔记十三: Map集合
    Go语言学习笔记十二: 范围(Range)
    Go语言学习笔记十一: 切片(slice)
    Go语言学习笔记十: 结构体
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10031447.html
Copyright © 2011-2022 走看看