POJ3261:Milk Patterns(后缀数组)
来源:互联网 发布:中国产业生产率数据库 编辑:程序博客网 时间:2024/06/06 07:51
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
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 212323231
Sample Output
4
Source
题意:找出最长的重复至少K次的子串。
思路:后缀数组(利用了相同前缀的后缀一定排在一起的性质)+二分答案即可,数据范围1e6离散化会好一点。
# include <iostream># include <cstdio># include <cstring># include <algorithm>#define LL long long#define N 20005using namespace std;int wa[N],wb[N],wsf[N],wv[N],sa[N];int Rank[N],height[N],s[N],ss[N], a[N],n,id[N],kk;char str1[N],str2[N];int cmp(int *r,int a,int b,int k){ return r[a]==r[b]&&r[a+k]==r[b+k];}void getsa(int *r,int *sa,int n,int m){ int i,j,p,*x=wa,*y=wb,*t; for(i=0; i<m; i++) wsf[i]=0; for(i=0; i<n; i++) wsf[x[i]=r[i]]++; for(i=1; i<m; i++) wsf[i]+=wsf[i-1]; for(i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i; p=1; j=1; for(; p<n; j*=2,m=p) { for(p=0,i=n-j; i<n; i++) y[p++]=i; for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0; i<n; i++) wv[i]=x[y[i]]; for(i=0; i<m; i++) wsf[i]=0; for(i=0; i<n; i++) wsf[wv[i]]++; for(i=1; i<m; i++) wsf[i]+=wsf[i-1]; for(i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i]; t=x; x=y; y=t; x[sa[0]]=0; for(p=1,i=1; i<n; i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++; }}void getheight(int *r,int n){ int i,j,k=0; for(i=1; i<=n; i++) Rank[sa[i]]=i; for(i=0; i<n; i++) { if(k) k--; else k=0; j=sa[Rank[i]-1]; while(r[i+k]==r[j+k]) k++; height[Rank[i]]=k; }}bool check(int x){ int cnt = 1; for(int i=2; i<=n; ++i) { if(height[i]>=x) { ++cnt; continue; } if(cnt>=kk) return true; else cnt=1; } return cnt>=kk;}bool cmp2(int x, int y){ return ss[x] < ss[y];}int main(){ scanf("%d%d",&n,&kk); int li = 0, imax = 0; for(int i=0; i<n; ++i) scanf("%d",&ss[i]), id[i]=i; sort(id, id+n, cmp2); s[id[0]] = ++li; for(int i=1; i<n; ++i) { if(ss[id[i]] != ss[id[i-1]]) ++li; s[id[i]] = li; } s[n] = 0; getsa(s,sa,n+1,li+1); getheight(s,n); int L=0, R=n; while(L<=R) { int mid = L+R>>1; if(check(mid)) L = mid+1; else R = mid-1; } printf("%d\n",R); return 0;}
- 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 后缀数组
- 数据预处理(2)—— One-hot coding 独热编码#分别使用 pandans.dummies 和 sklearn.feature_extraction.DictVectorizer 进行处理
- 社群会员必看:你关心的问题,答案都在这里了
- Spring Boot启动流程
- Linux之whereis命令
- Java基础------运算符
- POJ3261:Milk Patterns(后缀数组)
- 论文记录_MobileNets Efficient Convolutional Neural Networks for Mobile Vision Application
- Dockerfile解读
- java将数据库里的数据导出到excel(转)
- centos7 设置系统时间与网络同步
- MySQL查询本周、上周、本月、上个月份数据的sql代码
- SASS学习系列之四--------- 快速入门
- 手把手练习--cmake+VS2015编译OpenCV3.3源码(x86版本)
- Oracle游标使用方法及语法大全