poj3261 Milk Patterns(后缀数组)
来源:互联网 发布:天庭淘宝店无常 编辑:程序博客网 时间:2024/06/06 08:30
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 12571
Accepted: 5572
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 milkon day i appears on the ith line.
Output
Line 1: One integer, the length of the longest patternwhich occurs at least K times
Sample Input
8 2
1
2
3
2
3
2
3
1
Sample Output
4
Source
USACO2006 December Gold
【思路】
至少出现k次的可重叠最长子串。
二分长度+划分height,然后判断是否存在一组的数目不小于k即可。
需要注意的是:用末尾添0的方法解决n==1时的RE问题,但是在can中需要忽略height[0]且考虑height[1..n]。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 5 using namespace std; 6 7 const int maxn = 20000+10; 8 const int maxm = 1000000+10; 9 10 int s[maxn];11 int sa[maxn],c[maxm],t[maxn],t2[maxn];12 13 void build_sa(int m,int n) {14 int i,*x=t,*y=t2;15 for(i=0;i<m;i++) c[i]=0;16 for(i=0;i<n;i++) c[x[i]=s[i]]++;17 for(i=1;i<m;i++) c[i]+=c[i-1];18 for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;19 20 for(int k=1;k<=n;k<<=1) {21 int p=0;22 for(i=n-k;i<n;i++) y[p++]=i;23 for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;24 25 for(i=0;i<m;i++) c[i]=0;26 for(i=0;i<n;i++) c[x[y[i]]]++;27 for(i=0;i<m;i++) c[i]+=c[i-1];28 for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];29 30 swap(x,y);31 p=1; x[sa[0]]=0;32 for(i=1;i<n;i++) 33 x[sa[i]]=y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;34 if(p>=n) break;35 m=p;36 }37 }38 int rank[maxn],height[maxn];39 void getHeight(int n) {40 int i,j,k=0;41 for(i=0;i<=n;i++) rank[sa[i]]=i;42 for(i=0;i<n;i++) {43 if(k) k--;44 j=sa[rank[i]-1];45 while(s[j+k]==s[i+k]) k++;46 height[rank[i]]=k;47 }48 }49 50 int n,k;51 52 bool can(int limit) {53 int cnt=1; //忽略height[0] 截至height[n]54 for(int i=2;i<=n;i++) { 55 if(height[i]>=limit) {56 if(++cnt>=k) return true;57 }58 else cnt=1;59 }60 return false;61 }62 63 int main() {64 scanf("%d%d",&n,&k);65 int up=0;66 FOR(i,0,n-1) {67 scanf("%d",&s[i]);68 s[i]++;69 up=max(up,s[i]);70 }71 s[n]=0;72 build_sa(up+1,n);73 getHeight(n);74 int L=0,R=n;75 while(L<R) {76 int M=L+(R-L+1)/2;77 if(can(M)) L=M;78 else R=M-1;79 }80 printf("%d\n",L);81 return 0;82 }
- poj3261 Milk Patterns (后缀数组)
- POJ3261---Milk Patterns(后缀数组+二分)
- poj3261 Milk Patterns(后缀数组)
- Milk Patterns POJ3261 (后缀数组)
- POJ3261:Milk Patterns(后缀数组)
- 【后缀数组】Milk Patterns POJ3261
- poj3261 Milk Patterns 后缀数组
- 【POJ3261】Milk Patterns 后缀数组
- POJ3261:Milk Patterns(后缀数组)
- 后缀数组 - poj3261 Milk Patterns
- 【POJ3261】 Milk Patterns 后缀数组
- [后缀数组]poj3261 Milk Patterns
- POJ3261 Milk Patterns 【后缀数组】
- POJ3261——Milk Patterns(后缀数组)
- 【POJ3261】Milk Patterns【后缀数组】【二分】
- 【poj3261】Milk Patterns 后缀数组+二分
- poj3261 Milk Patterns 后缀数组+二分
- POJ3261——Milk Patterns 后缀数组
- 1019. 数字黑洞
- Android知识库
- poj1743 Musical Theme(后缀数组|后缀自动机)
- 正则表达式中的匹配,替换,切割,获取,这四个使用时具体用哪一个,进行案例分析
- 数据结构 - 简单的单链表结构
- poj3261 Milk Patterns(后缀数组)
- SQL 查询地理信息中指定距离范围内的记录
- Ural1297 Palindrome(后缀数组)
- android学习四(Activity的生命周期)
- poj2406 Power Strings(kmp失配函数)
- 求解二叉树的深度(高度)_C语言
- poj2774 Long Long Message(后缀数组)
- [总结]后缀数组: 注释+模板
- 1020. 月饼