• 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;
    }
    
  • 相关阅读:
    Android实现资料收藏
    GPS(Global Positioning System)全球定位系统
    Android开发环境搭建全程演示(jdk+eclipse+android sdk)
    ADT在线安装
    简洁判断一个byte中有多少位为1的bit?
    tomcat部署java项目
    centos7把编译安装的服务通过systemctl管理
    ansible自动化部署
    redi通过哨兵sentinel实现主从切换
    redis实现集群加主从复制
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10031447.html
走看看 - 开发者的网上家园