• poj3261 Milk Patterns


    Milk Patterns
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 17196   Accepted: 7612
    Case Time Limit: 2000MS

    Description

    Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

    To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

    Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

    Input

    Line 1: Two space-separated integers: N and K 
    Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

    Output

    Line 1: One integer, the length of the longest pattern which occurs at least K times

    Sample Input

    8 2
    1
    2
    3
    2
    3
    2
    3
    1

    Sample Output

    4

    Source

    题目大意:求一个最长的重复出现了至少k次的连续子串,它们可以重叠.
    分析:类似poj1743的方法,还是二分+分组.在check函数上改动一下就好了.
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 30010;
    int n,s[maxn],ans,fir[maxn],sec[maxn],pos[maxn],sa[maxn],rk[maxn],tong[maxn],ht[maxn];
    int sett[maxn],a[maxn],cnt,K;
    
    void solve()
    {
        int len = n;
        memset(rk,0,sizeof(rk));
        memset(sa,0,sizeof(sa));
        memset(ht,0,sizeof(ht));
        memset(fir,0,sizeof(fir));
        memset(sec,0,sizeof(sec));
        memset(pos,0,sizeof(pos));
        memset(tong,0,sizeof(tong));
        copy(s + 1,s + len + 1,sett + 1);
        sort(sett + 1,sett + 1 + len);
        cnt = unique(sett + 1,sett + 1 + len) - sett - 1;
        for (int i = 1; i <= len; i++)
            a[i] = lower_bound(sett + 1,sett + 1 + cnt,s[i]) - sett;
        for (int i = 1; i <= len; i++)
            tong[a[i]]++;
        for (int i = 1; i <= len; i++)
            tong[i] += tong[i - 1];
        for (int i = 1; i <= len; i++)
            rk[i] = tong[a[i] - 1] + 1;
        for (int t = 1; t <= len; t *= 2)
        {
            for (int i = 1; i <= len; i++)
                fir[i] = rk[i];
            for (int i = 1; i <= len; i++)
            {
                if (i + t > len)
                    sec[i] = 0;
                else
                    sec[i] = rk[i + t];
            }
            fill(tong,tong + 1 + len,0);
            for (int i = 1; i <= len; i++)
                tong[sec[i]]++;
            for (int i = 1; i <= len; i++)
                tong[i] += tong[i - 1];
            for (int i = 1; i <= len; i++)
                pos[len - --tong[sec[i]]] = i;
            fill(tong,tong + 1 + len,0);
            for (int i = 1; i <= len; i++)
                tong[fir[i]]++;
            for (int i = 1; i <= len; i++)
                tong[i] += tong[i - 1];
            for (int i = 1; i <= len; i++)
            {
                int temp = pos[i];
                sa[tong[fir[temp]]--] = temp;
            }
            bool flag = true;
            int last = 0;
            for (int i = 1; i <= len; i++)
            {
                int temp = sa[i];
                if (!last)
                    rk[temp] = 1;
                else if (fir[temp] == fir[last] && sec[temp] == sec[last])
                {
                    rk[temp] = rk[last];
                    flag = false;
                }
                else
                    rk[temp] = rk[last] + 1;
                last = temp;
            }
            if (flag)
                break;
        }
        int k = 0;
        for (int i = 1; i <= len; i++)
        {
            if (rk[i] == 1)
                k = 0;
            else
            {
                if (k)
                    k--;
                int j = sa[rk[i] - 1];
                while (i + k <= len && j + k <= len && a[i + k] == a[j + k])
                    k++;
            }
            ht[rk[i]] = k;
        }
    }
    
    bool check(int x)
    {
        int pos = 1;
        for (int i = 2; i <= n; i++)
        {
            if (ht[i] >= x)  //代表重复子串的长度
            {
                if (i - pos + 1 >= K)
                    return true;
                continue;
            }
            pos = i;
        }
        return false;
    }
    
    int main()
    {
        scanf("%d%d",&n,&K);
        for (int i = 1; i <= n; i++)
            scanf("%d",&s[i]);
        solve();
        int l = 1,r = n;
        while (l <= r)
        {
            int mid = (l + r) >> 1;
            if (check(mid))
            {
                ans = mid;
                l = mid + 1;
            }
            else
                r = mid - 1;
        }
        printf("%d
    ",ans);
    
        return 0;
    }
  • 相关阅读:
    Java-Android 之动画的实现
    Java-Android 之出滚动条和卷轴页面
    Java-Android 之页面的跳转和结构的搭建
    Java-Android 之Hello World
    Java-struts2 之值栈问题
    Java-Hirbernate中文乱码问题
    Java-struts2 之中文乱码问题
    SQL SERVER2005事务日志已满 解决方法
    解决:对 PInvoke 函数的调用导致堆栈不对称问题
    webclient下载文件 带进度条
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8544514.html
Copyright © 2020-2023  润新知