POJ3261 Milks 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 Ktimes.
Input
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
题解:
题目意思是让求出现次数超过k次的子串的最长长度;
思路:后缀数组,然后二分答案,求次数大于k的最长的子串;
参考代码:

1 //#include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<string> 6 #include<cstdlib> 7 #include<cmath> 8 #include<algorithm> 9 using namespace std; 10 #define clr(a,val) memset(a,val,sizeof(a)) 11 #define lowbit(x) x&-x 12 typedef long long ll; 13 const int INF=0x3f3f3f3f; 14 inline int read() 15 { 16 int x=0,f=1;char ch=getchar(); 17 while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} 18 while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar();} 19 return x*f; 20 } 21 const int maxn=1e6+10; 22 int N,K; 23 struct SuffixArray{ 24 int s[maxn]; 25 int sa[maxn],height[maxn],rank[maxn],n; 26 int t[maxn*2],t2[maxn*2]; 27 int cnt[maxn]; 28 void build_sa(int m)//字符都属于0~m-1范围 29 { 30 int i,*x=t,*y=t2; 31 for(i=0;i<m;i++) cnt[i]=0; 32 for(i=0;i<n;i++) cnt[x[i]=s[i]]++; 33 for(i=1;i<m;i++) cnt[i]+=cnt[i-1]; 34 for(i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i; 35 for(int k=1,p;k<=n;k <<=1)//k<=n 36 { 37 p=0; 38 for(i=n-k;i<n;i++) y[p++]=i; 39 for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k; 40 for(i=0;i<m;i++) cnt[i]=0; 41 for(i=0;i<n;i++) cnt[x[y[i]]]++; 42 for(i=1;i<m;i++) cnt[i]+=cnt[i-1]; 43 for(i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i]; 44 swap(x,y); 45 p=1;x[sa[0]]=0; 46 for(i=1;i<n;i++) 47 x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]? p-1:p++; 48 if(p>=n) break; 49 m=p; 50 } 51 } 52 void build_height() 53 { 54 int k=0; 55 for(int i=0;i<n;i++) rank[sa[i]]=i; 56 for(int i=0;i<n-1;i++) 57 { 58 if(k) k--; 59 int j=sa[rank[i]-1]; 60 while(s[i+k]==s[j+k]) k++; 61 height[rank[i]]=k; 62 } 63 } 64 } SA; 65 66 inline bool check(int ans) 67 { 68 int cnt=1; 69 for(int i=2;i<SA.n;++i) 70 { 71 if(SA.height[i]>=ans) cnt++; 72 else 73 { 74 if(cnt>=K) return true; 75 cnt=1; 76 } 77 } 78 return false; 79 } 80 81 int main() 82 { 83 N=read();K=read(); 84 for(int i=0;i<N;++i) 85 { 86 SA.s[i]=read();SA.s[i]++; 87 } 88 SA.s[N++]=0;SA.n=N; 89 SA.build_sa(maxn); 90 SA.build_height(); 91 int l=0,r=N; 92 while(l<=r) 93 { 94 int mid=l+r>>1; 95 if(check(mid)) l=mid+1; 96 else r=mid-1; 97 } 98 cout<<r<<endl; 99 return 0; 100 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· Ai满嘴顺口溜,想考研?浪费我几个小时
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想