poj3261 Milk Patterns (哈希+二分)

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
给一个长度为N的串,找到这个串所有出现次数大于k的子串中找到长度最长的那个,并且输出长度。
思路:这里有一个类似于前缀和的思想(之前也碰到过一次,好像异或也可以类似于前缀和来维护前缀异或值),用hash[i]表示从1到i的子串所对应的哈希值,将每个子串都看为10进制数,然后对于某一个子串我们就可以O(1)的获得其对应的哈希值。那么我们就只需要倒序枚举长度len,看当前长度的子串中出现次数(就是其对应哈希值的出现次数)有没有超过k的,有就直接break得到答案;然后t了。。。
事实上,对于枚举的长度是存在一种单调性的,如果枚举的某个长度l是可行的,那么所有小于l的都是可行的,我们就只需要检查大于l的长度中是否还有满足条件的。所以应该用二分答案
还有这道题不知道是不是卡map,我用map记录出现次数过不了T-T

//#include<bits/stdc++.h>
#include<map>
#include <algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 1000000+5
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
typedef long long ll;
typedef long double ld;
const int inf_max = 0x3f3f3f;
const ll Linf = 9e18;
const int maxn = 2e4+10;
const long double E = 2.7182818;
const double eps=0.0001;
using namespace std;
inline int read()
{
    int f=1,res=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { res=res*10+ch-'0' ; ch=getchar(); }
    return f*res;
}
int n,k,ans;
ll a[maxn],Hash[maxn],pval[maxn],cur[maxn];
const ll base = 10;
bool check(int i) {
    for(int l = 1;l <= n - i + 1; ++l) {
        int r = l + i - 1;
        cur[l] = (Hash[r] - (Hash[l - 1] * pval[r + 1 - l] % mod) % mod + mod) % mod;  
    }
    // printf("%d %lld,%lld\n",n - i + 1,cur[1],cur[2]);
    sort(cur + 1,cur + n - i + 2);
    int maxv = 1,cnt = 1;
    ll num = cur[1];
    for(int j = 2;j <= n - i + 1; ++j) {  //统计相同的数出现的次数,取满足条件的最大值
        if(cur[j] != cur[j - 1]) {
            num = cur[j];
            maxv = max(maxv,cnt);
            cnt = 1;
        }
        else ++cnt;
    }
    //printf("cnt=%d\n",cnt);
    maxv = max(maxv,cnt);
    return (maxv >= k);
}
int main()
{
    while(~scanf("%d%d",&n,&k)) {
        ans = 0;
        memset(Hash,0,sizeof(Hash));
        for(int i = 1;i <= n; ++i) {
            scanf("%lld",&a[i]);
        }
        pval[0] = 1;
        for(int i = 1;i <= n; ++i) {
            pval[i] = pval[i - 1] * base % mod;  //预处理一下后面要用的值
            Hash [i] = (Hash[i - 1] * base % mod + a[i]) % mod;
        }
        int L = 1,R = n;
        while(L <= R) {
            int mid = (L + R) >> 1;
            //printf("mid=%d %d\n",mid,check(mid));
            if(check(mid)) {
                ans = mid;
                L = mid + 1;
            }else R = mid - 1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
发布了33 篇原创文章 · 获赞 14 · 访问量 408

猜你喜欢

转载自blog.csdn.net/qq_44077455/article/details/104031357