zoukankan      html  css  js  c++  java
  • hdu Boring counting

    Boring counting

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 120    Accepted Submission(s): 52
     
    Problem Description
    035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other. Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
     
    Input
    The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
     
    Output
    For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
     
    Sample Input
    aaaa
    ababcabb
    aaaaaa
    #
     
    Sample Output
    2
    3
    3
     
     
    Source
    2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
     
    Recommend
    zhengfeng

    分析:倍增算法写的后缀数组。因为要求重复的子串数量,枚举子串长度,利用公共前缀height数组确定是否可行。

    #include<cstdio>
    #include<cstring>
    #define MAXN 500010
    #define maxn 1010
    int ws[MAXN], wv[MAXN], wa[MAXN], wb[MAXN], sa[MAXN], rank[MAXN];
    int num[maxn], height[maxn];
    char str[maxn];
    int m;
    
    int min(int a, int b) {
        return a < b ? a : b;
    }
    
    int max(int a, int b) {
        return a > b ? a : b;
    }
    
    void make_sa(int n) {
        int i, j, *x, *y, *t, p;
        x = wa;
        y = wb;
        for (i = 0; i < m; ++i)
            ws[i] = 0;
        for (i = 0; i < n; ++i)
            ws[x[i] = num[i]]++;
        for (i = 1; i < m; ++i)
            ws[i] += ws[i - 1];
        for (i = n - 1; i >= 0; --i)
            sa[--ws[x[i]]] = i;
        p = 1;
        for (j = 1; p < n; j *= 2) {
            p = 0;
            for (i = n - j; i < n; ++i) {
                y[p++] = i;
            }
            for (i = 0; i < n; ++i) {
                if (sa[i] >= j)
                    y[p++] = sa[i] - j;
            }
            for (i = 0; i < n; ++i) {
                wv[i] = x[y[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)
                sa[--ws[wv[i]]] = y[i];
            t = x;
            x = y;
            y = t;
            p = 1;
            x[sa[0]] = 0;
            for (i = 1; i < n; ++i) {
                if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j])
                    x[sa[i]] = p - 1;
                else
                    x[sa[i]] = p++;
            }
            m = p;
        }
    }
    
    void make_height(int n) {
        int i, j, k = 0;
        for (i = 1; i <= n; ++i)
            rank[sa[i]] = i;
        for (i = 0; i < n; ++i) {
            if (k)
                --k;
            j = sa[rank[i] - 1];
            while (num[i + k] == num[j + k])
                ++k;
            height[rank[i]] = k;
        }
    }
    
    int tot(int len, int n) {
        int i, mmin, mmax, cnt;
        mmin = 1 << 30;
        mmax = -1;
        cnt = 0;
        for (i = 2; i <= n; ++i) {
            //    printf("height=%d len=%d\n", height[i], len);
            if (height[i] >= len) {
                mmax = max(max(sa[i], sa[i - 1]), mmax);
                mmin = min(min(sa[i], sa[i - 1]), mmin);
                //      printf("mm %d %d\n", mmin, mmax);
            } else {
                if (mmax - mmin >= len) {
                    ++cnt;
                }
                mmin = 1 << 30;
                mmax = -1;
            }
        }
        if (mmax - mmin >= len) {
            ++cnt;
        }
        return cnt;
    }
    
    int main() {
        int i, n, sum;
        while (scanf("%s", str), str[0] != '#') {
            sum = 0;
            m = 30;
            n = strlen(str);
            for (i = 0; i < n; ++i)
                num[i] = str[i] - 'a' + 1;
            num[n] = 0;
            make_sa(n + 1);
            make_height(n);
            for (i = 1; i <= n / 2; ++i)
                sum += tot(i, n);
            printf("%d\n", sum);
        }
        return 0;
    }
  • 相关阅读:
    内置方法(item系列、__str__方法、__del__方法)
    POJ3436
    CF551B
    HDU1588
    HDU3117
    CF834D
    CF832D
    CF832C
    POJ1930
    POJ3666
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2687129.html
Copyright © 2011-2022 走看看