• B P5 第十三届北航程序设计竞赛预赛


    https://buaacoding.cn/contest-ng/index.html#/188/problems

    其实这题挺简单的。

    注意到答案的大小最多是22

    二分,check长度是mid的不同子串有多少个,然后,如果不是1 << mid个,那么肯定是不行的。

    想到了这个,我居然用了字符串hash去判,傻逼了,注意到是01串,相当于二进制。

    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 5e6 + 20;
    typedef unsigned long long int ULL;
    char str[maxn];
    int lenstr;
    int cnt[1 << 23], DFN;
    int po[maxn];
    bool check(int mid) {
        DFN++;
        int val = 0;
        for (int i = 1; i <= mid; ++i) {
            val = val * 2 + str[i] - '0';
        }
        cnt[val] = DFN;
        for (int i = mid + 1; i <= lenstr; ++i) {
            val -= po[mid - 1] * (str[i - mid] - '0');
            val *= 2;
            val += str[i] - '0';
            cnt[val] = DFN;
        }
        int en = (1 << mid) - 1;
        for (int i = 0; i <= en; ++i) {
            if (cnt[i] != DFN) {
                return true;
            }
        }
        return false;
    }
    void work() {
        scanf("%s", str + 1);
        lenstr = strlen(str + 1);
        int be = 1, en = min(22, lenstr);
        while (be <= en) {
            int mid = (be + en) >> 1;
            if (check(mid)) en = mid - 1;
            else be = mid + 1;
        }
        printf("%d
    ", be);
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        po[0] = 1;
        for (int i = 1; i <= 23; ++i) po[i] = po[i - 1] * 2;
        int t;
        scanf("%d", &t);
        while (t--) work();
        return 0;
    }
    View Code

    很奇怪为什么能卡我dc3,我能算出长度是i的不同子串的个数,

    思路就是,每一个sa[i],有lenstr - sa[i] + 1个前缀,然后和前面的sa[i - 1] 会有重叠的地方,也就是height[i]个,然后减去即可。

    区间减法打标记。但是,,TLE额。

    why

    可能多了点常数

    /* 
     Author: stupid_one
     Result: TLE    Submission_id: 506947
     Created at: Sun Dec 17 2017 10:45:12 GMT+0800 (CST)
     Problem_id: 959    Time: 1000    Memory: 0
    */
    
    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 9e6 + 20;
    typedef unsigned long long int ULL;
    char str[maxn];
    int lenstr;
    int cnt[maxn];
    
    const int N = maxn;
    #define F(x) ((x)/3+((x)%3==1?0:tb))
    #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
    int r[maxn];
    int wa[maxn],wb[maxn],wv[maxn],WS[maxn];
    int sa[maxn];
    int c0(int *r,int a,int b) {
        return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
    }
    int c12(int k,int *r,int a,int b) {
        if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
        else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
    }
    void sort(int *r,int *a,int *b,int n,int m) {
        int i;
        for(i=0; i<n; i++) wv[i]=r[a[i]];
        for(i=0; i<m; i++) WS[i]=0;
        for(i=0; i<n; i++) WS[wv[i]]++;
        for(i=1; i<m; i++) WS[i]+=WS[i-1];
        for(i=n-1; i>=0; i--) b[--WS[wv[i]]]=a[i];
        return;
    }
    void dc3(int *r,int *sa,int n,int m) { //涵义与DA 相同
        int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
        r[n]=r[n+1]=0;
        for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i;
        sort(r+2,wa,wb,tbc,m);
        sort(r+1,wb,wa,tbc,m);
        sort(r,wa,wb,tbc,m);
        for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++)
            rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
        if(p<tbc) dc3(rn,san,tbc,p);
        else for(i=0; i<tbc; i++) san[rn[i]]=i;
        for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3;
        if(n%3==1) wb[ta++]=n-1;
        sort(r,wb,wa,ta,m);
        for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;
        for(i=0,j=0,p=0; i<ta && j<tbc; p++)
            sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
        for(; i<ta; p++) sa[p]=wa[i++];
        for(; j<tbc; p++) sa[p]=wb[j++];
        return;
    }
    int RANK[maxn], height[maxn];
    void calheight(int *r,int *sa,int n) { // 此处N为实际长度
        int i,j,k=0;        // height[]的合法范围为 1-N, 其中0是结尾加入的字符
        for(i=1; i<=n; i++) RANK[sa[i]]=i; // 根据SA求RANK
        for(i=0; i<n; height[RANK[i++]] = k ) // 定义:h[i] = height[ rank[i] ]
            for(k?k--:0,j=sa[RANK[i]-1]; r[i+k]==r[j+k]; k++); //根据 h[i] >= h[i-1]-1 来优化计算height过程
    }
    void work() {
        scanf("%s", str);
        lenstr = strlen(str);
        
        for (int i = 0; i < lenstr; ++i) r[i] = str[i];
        r[lenstr] = 0;
        dc3(r, sa, lenstr + 1, 128);
        calheight(r, sa, lenstr);
    //    printf("%d
    ", sa[1]);
        for (int i = 1; i <= lenstr; ++i) {
            cnt[1]++;
            cnt[lenstr - sa[i] + 1]--;
            cnt[1]--;
            cnt[height[i] + 1]++;
        }
        for (int i = 1; i < 23; ++i) cnt[i] += cnt[i - 1];
        for (int i = 1; i <= 22; ++i) {
            if (cnt[i] != (1 << (i))) {
                printf("%d
    ", i);
                for (int k = 1; k < 23; ++k) cnt[k] = 0;
                return;
            }
        }
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        int t;
        scanf("%d", &t);
        while (t--) work();
        return 0;
    }
    View Code
    /* 
     Author: stupid_one
     Result: TLE    Submission_id: 506775
     Created at: Sun Dec 17 2017 01:21:28 GMT+0800 (CST)
     Problem_id: 959    Time: 1000    Memory: 0
    */
    
    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 5e6 + 20;
    typedef unsigned long long int ULL;
    ULL po[maxn];
    char str[maxn];
    int lenstr;
    ULL hs[maxn];
    const int seed = 131;
    const int mod = 10007;
    struct MP {
        int first[mod + 2], num;
        ULL eval[maxn];
        int nxt[maxn];
        void init() {
            memset(first, -1, sizeof first);
            num = 0;
        }
        bool addEdge(ULL val) {
            int u = val % mod;
            for (int i = first[u]; ~i; i = nxt[i]) {
                if (val == eval[i]) return false;
            }
            eval[num] = val;
            nxt[num] = first[u];
            first[u] = num++;
            return true;
        }
    } h;
    bool check(int val) {
        int t = 1 << val;
        if (lenstr - val + 1 < t) return false;
        h.init();
        int ans = 0;
        for (int i = val; i <= lenstr; ++i) {
            ans += h.addEdge(hs[i] - po[val] * hs[i - val]);
            if (ans == t) return true;
            if (ans + (lenstr - i) < t) return false;
        }
        return false;
    }
    int sa[maxn], x[maxn], y[maxn], book[maxn]; //book[]大小起码是lenstr,book[rank[]]
    bool cmp(int r[], int a, int b, int len) { //这个必须是int r[],
        return r[a] == r[b] && r[a + len] == r[b + len];
    }
    void da(char str[], int sa[], int lenstr, int mx) {
        int *fir = x, *sec = y, *ToChange;
        for (int i = 0; i <= mx; ++i) book[i] = 0; //清0
        for (int i = 1; i <= lenstr; ++i) {
            fir[i] = str[i];  //开始的rank数组,只保留相对大小即可,开始就是str[]
            book[str[i]]++; //统计不同字母的个数
        }
        for (int i = 1; i <= mx; ++i) book[i] += book[i - 1]; //统计 <= 这个字母的有多少个元素
        for (int i = lenstr; i >= 1; --i) sa[book[fir[i]]--] = i;
    // <=str[i]这个字母的有x个,那么,排第x的就应该是这个i的位置了。
        //倒过来排序,是为了确保相同字符的时候,前面的就先在前面出现。
        //p是第二个关键字0的个数
        for (int j = 1, p = 1; p <= lenstr; j <<= 1, mx = p) {  //字符串长度为j的比较
            //现在求第二个关键字,然后合并(合并的时候按第一关键字优先合并)
            p = 0;
            for (int i = lenstr - j + 1; i <= lenstr; ++i) sec[++p] = i;
    //上面的位置,再跳j格就是越界了的,所以第二关键字是0,排在前面
            for (int i = 1; i <= lenstr; ++i)
                if (sa[i] > j)  //如果排名第i的起始位置在长度j之后
                    sec[++p] = sa[i] - j;
    //减去这个长度j,表明第sa[i] - j这个位置的第二个是从sa[i]处拿的,排名靠前也//正常,因为sa[i]排名是递增的
            //sec[]保存的是下标,现在对第一个关键字排序
            for (int i = 0; i <= mx; ++i) book[i] = 0; //清0
            for (int i = 1; i <= lenstr; ++i) book[fir[sec[i]]]++;
            for (int i = 1; i <= mx; ++i) book[i] += book[i - 1];
            for (int i = lenstr; i >= 1; --i) sa[book[fir[sec[i]]]--] = sec[i];
    //因为sec[i]才是对应str[]的下标
    //现在要把第二关键字的结果,合并到第一关键字那里。同时我需要用到第一关键//字保存的记录,所以用指针交换的方式达到快速交换数组中的值
            ToChange = fir, fir = sec, sec = ToChange;
            fir[sa[1]] = 0; //固定的是0 因为sa[1]固定是lenstr那个0
            p = 2;
            for (int i = 2; i <= lenstr; ++i)  //fir是当前的rank值,sec是前一次的rank值
                fir[sa[i]] = cmp(sec, sa[i - 1], sa[i], j) ? p - 1 : p++;
        }
        return ;
    }
    int height[maxn], RANK[maxn];
    void calcHight(char str[], int sa[], int lenstr) {
        for (int i = 1; i <= lenstr; ++i) RANK[sa[i]] = i; //O(n)处理出rank[]
        int k = 0;
        for (int i = 1; i <= lenstr - 1; ++i) {
    //最后一位不用算,最后一位排名一定是1,然后sa[0]就尴尬了
            k -= k > 0;
            int j = sa[RANK[i] - 1]; //排名在i前一位的那个串,相似度最高
            while (str[j + k] == str[i + k]) ++k;
            height[RANK[i]] = k;
        }
        return ;
    }
    int cnt[maxn];
    void work() {
        scanf("%s", str + 1);
        lenstr = strlen(str + 1);
        for (int i = 1; i <= lenstr; ++i) cnt[i] = 0;
        str[lenstr + 1] = '$';
        str[lenstr + 2] = 0;
        da(str, sa, lenstr + 1, 128);
        calcHight(str, sa, lenstr + 1);
        for (int i = 2; i <= lenstr + 1; ++i) {
            cnt[1]++;
            cnt[lenstr - sa[i] + 2]--;
            cnt[1]--;
            cnt[height[i] + 1]++;
        }
        for (int i = 1; i <= lenstr; ++i) cnt[i] += cnt[i - 1];
        for (int i = 1; i <= lenstr; ++i) {
            if (cnt[i] != (1 << i)) {
                printf("%d
    ", i);
                return;
            }
        }
    //    printf("
    ");
    //    for (int i = 1; str[i]; ++i) hs[i] = hs[i - 1] * seed + str[i];
    //    for (int i = 1; i <= 22; ++i) {
    //        if (!check(i)) {
    //            printf("%d
    ", i);
    //            return ;
    //        }
    //    }
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        po[0] = 1;
        for (int i = 1; i <= maxn - 20; ++i) po[i] = po[i - 1] * seed;
        int t;
        scanf("%d", &t);
        while (t--) work();
        return 0;
    }
    da
  • 相关阅读:
    4500 小Q系列故事——屌丝的逆袭
    HDU 1171 Big Event in HDU
    linux库文件 /usr/lib
    HDU 1860 统计字符
    编程之美~~传话游戏
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1203 I NEED A OFFER!
    各种树
    HDU 3127 WHUgirls
    01背包 完全背包 多重背包 二维费用背包
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/8051597.html
Copyright © 2020-2023  润新知