USACO06DEC Milk Patterns——Solution

题目描述

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.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

                            --by luogu

https://daniu.luogu.org/problem/show?pid=2852



后缀数组查询可重叠出现超过k次的最长子串长度;

可以等价于height数组中所有长度为k-1的区间最小值的最大值;

不严谨的证明:

合法性:

某区间最小值即为该区间和其左端点减一所有子串的公共前缀;

于是她的确出现的k次,是合法的答案;

最优性:

设子串 x为最优答案;

她满足作为k+个后缀的前缀;

则这k+个后缀rank连续,即她们中第一个除外的height可构成一个长度大于等于k-1的区间;

而这个区间会把x贡献到答案上(否则意味着该区间min不是x——与“作为这k+个后缀的前缀”的条件矛盾)

于是得到结论:可以等价于height数组中所有长度为k-1的区间最小值的最大值;

于是可以使用O(n)的单调队列;

然而为什么在luogu的数据下O(nlogn)的二分len的方法并不慢于单调队列?

值得一提的,由于某(shu)些(ju)原(tai)因(shui)本来可能很大的

其实很小,(20多?30多?)......

代码如下:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int MAXN=20010;
 5 int n,m=30,k;
 6 int x[MAXN],y[MAXN],c[MAXN],s[MAXN];
 7 int sa[MAXN],rank[MAXN],height[MAXN];
 8 int que[100010],h,t;
 9 inline void build_sa();
10 inline void build_height();
11 inline void in(int &ans)
12 {
13     ans=0;bool p=false;char ch=getchar();
14     while((ch>'9' || ch<'0')&&ch!='-') ch=getchar();
15     if(ch=='-') p=true,ch=getchar();
16     while(ch<='9'&&ch>='0') ans=ans*10+ch-'0',ch=getchar();
17     if(p) ans=-ans;
18 }
19 int main()
20 {
21     int i,j,k,ans=0;
22     in(n),in(k);
23     for(i=0;i<n;i++)
24         in(s[i]);
25     build_sa();
26     build_height();
27     h=0,t=0;
28     for(i=0;i<n;i++){
29         while(h<t&&height[que[t]]>height[i])t--;
30         que[++t]=i;
31         if(i>=k-2){
32             if(height[que[h+1]]>ans)ans=height[que[h+1]];
33             if(que[h+1]<=i-k+2)h++;
34         }
35     }
36     printf("%d",ans);
37     return 0;
38 }
39 inline void build_sa(){
40     int i,j,k;
41     for(i=1;i<=m;i++)c[i]=0;
42     for(i=0;i<n;i++)c[x[i]=s[i]]++;
43     for(i=2;i<=m;i++)c[i]+=c[i-1];
44     for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
45     for(k=1;k<=n;k<<=1){
46         int num=0;
47         for(i=n-k;i<n;i++)y[num++]=i;
48         for(i=0;i<n;i++)if(sa[i]>=k)y[num++]=sa[i]-k;
49         for(i=1;i<=m;i++)c[i]=0;
50         for(i=0;i<n;i++)c[x[i]]++;
51         for(i=2;i<=m;i++)c[i]+=c[i-1];
52         for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i],y[i]=0;
53         swap(x,y);
54         num=1;x[sa[0]]=1;
55         for(i=1;i<n;i++)
56             if(y[sa[i]]!=y[sa[i-1]]||y[sa[i]+k]!=y[sa[i-1]+k])
57                 x[sa[i]]=++num;
58             else
59                 x[sa[i]]=num;
60         if(num>n)break;
61         m=num;
62     }
63 }
64 inline void build_height(){
65     int i,j,k=0;
66     for(i=0;i<n;i++)rank[sa[i]]=i;
67     for(i=0;i<n;i++){
68         if(!rank[i])continue;
69         if(k)k--;
70         j=sa[rank[i]-1];
71         while(j+k<n&&i+k<n&&s[i+k]==s[j+k])k++;
72         height[rank[i]]=k;
73     }
74 }

祝AC

 

转载于:https://www.cnblogs.com/nietzsche-oier/p/6635589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值