POJ3261 MILK PATTERNS (array of suffixes)

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 ≤ KN) 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

Line 1: Two space-s eparated integers: N and K
Lines 2.. N+1: Nintegers, 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

Problem solution:

The question means let you find one in an array The substring makes it appear no less than K times. What is the longest length?

Suffix array + dichotomy: Use the nature of the height array of the suffix array to split the answer. (If you understand the suffix array and see the dichotomy, you should know how to write it)

Reference code:

share picture

#include

#include
<string.h>
#include

#include

#include

#include
<set>
using namespace std;
int A[20010],ha[20010];
int sa[20010],c[20010],ra[20010],x[20010],y[20010],h[20010< span style="color: #000000;">];
void build(int n,int m){
int i,j,p;
for(i=0;i0;
for(i=0;i){
x[i]=A[i]+1;
c[x[i]]
++;
}
for(i=1;i1];
for(i=0;ii;
for(j=1;j2){
p
=0;
for(i=nj;ii;
for(i=0;iif(sa[i]>=j) y[p++]=sa[i]- j;
for(i=0;i0;
for(i=0;i;
for(i=1;i1];
for(i=n-1;i> =0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[
0]]=0< span style="color: #000000;">;
m=1;
for(i=1;i)
{
if(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]) x [sa[i]]=m-1;
else x[sa[i]]=m++;
}
if(m>=n) break;
}
}
void height(int n)
{
int i,j,k=0;
for(i=0;i<=n ;i++) ra[sa[i]]=i;
for(i=0;i){
if(k) k--;
j
=sa[ra[i]-1];
while(A[i+k]==A[j+k]) k++;
h[ra[i]]
=k;
}
}
int check(int l,int n,int k){
int sum=1,ma=-1,i;
for(i=1;i<=n ;i++){
if(h[i]>=l){
sum
++;
if(masum;
}
else sum=1;
}
return ma>=k;
}
set<int>s;
set<int>::iterator it;
int main()
{
int i,n,k,m;
scanf(
"%d%d",&n,&k);
for(i=0;i)
{
scanf("%d",&A[i]);
s.insert(A[i]);
}
m
=0;
for(it=s.begin();it!=s.end();it++) ha[m++]= *it;
for(i=0;iha;
A[n]=-1;
build(n
+1,20005);
height(n);
int l=1,r=20005,mid;
while(l+1<r)
{
mid
=(l+r)/2;
if(check(mid,n,k)) l=mid ;
else r=mid;
}
if(check(l,n,k)) printf(" %d ",l);
else printf("0 ");
return 0;
}

View Code

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.

Line 1: One integer, the length of the longest pattern which occurs at least K times

share picture

#include

#include
<string.h>
#include

#include

#include

#include
<set>
using namespace std;
int A[20010],ha[20010];
int sa[20010],c[20010],ra[20010],x[20010],y[20010],h[20010< span style="color: #000000;">];
void build(int n,int m){
int i,j,p;
for(i=0;i0;
for(i=0;i){
x[i]=A[i]+1;
c[x[i]]
++;
}
for(i=1;i1];
for(i=0;ii;
for(j=1;j2){
p
=0;
for(i=nj;ii;
for(i=0;iif(sa[i]>=j) y[p++]=sa[i]- j;
for(i=0;i0;
for(i=0;i;
for(i=1;i1];
for(i=n-1;i> =0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[
0]]=0< span style="color: #000000;">;
m=1;
for(i=1;i)
{
if(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]) x [sa[i]]=m-1;
else x[sa[i]]=m++;
}
if(m>=n) break;
}
}
void height(int n)
{
int i,j,k=0;
for(i=0;i<=n ;i++) ra[sa[i]]=i;
for(i=0;i){
if(k) k--;
j
=sa[ra[i]-1];
while(A[i+k]==A[j+k]) k++;
h[ra[i]]
=k;
}
}
int check(int l,int n,int k){
int sum=1,ma=-1,i;
for(i=1;i<=n ;i++){
if(h[i]>=l){
sum
++;
if(masum;
}
else sum=1;
}
return ma>=k;
}
set<int>s;
set<int>::iterator it;
int main()
{
int i,n,k,m;
scanf(
"%d%d",&n,&k);
for(i=0;i)
{
scanf("%d",&A[i]);
s.insert(A[i]);
}
m
=0;
for(it=s.begin();it!=s.end();it++) ha[m++]= *it;
for(i=0;iha;
A[n]=-1;
build(n
+1,20005);
height(n);
int l=1,r=20005,mid;
while(l+1<r)
{
mid
=(l+r)/2;
if(check(mid,n,k)) l=mid ;
else r=mid;
}
if(check(l,n,k)) printf(" %d ",l);
else printf("0 ");
return 0;
}

View Code

#include

#include
<string.h>
#include

#include

#include

#include
<set>
using namespace std;
int A[20010],ha[20010];
int sa[20010],c[20010],ra[20010],x[20010],y[20010],h[20010< span style="color: #000000;">];
void build(int n,int m){
int i,j,p;
for(i=0;i0;
for(i=0;i){
x[i]=A[i]+1;
c[x[i]]
++;
}
for(i=1;i1];
for(i=0;ii;
for(j=1;j2){
p
=0;
for(i=nj;ii;
for(i=0;iif(sa[i]>=j) y[p++]=sa[i]- j;
for(i=0;i0;
for(i=0;i;
for(i=1;i1];
for(i=n-1;i> =0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[
0]]=0< span style="color: #000000;">;
m=1;
for(i=1;i)
{
if(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]) x [sa[i]]=m-1;
else x[sa[i]]=m++;
}
if(m>=n) break;
}
}
void height(int n)
{
int i,j,k=0;
for(i=0;i<=n ;i++) ra[sa[i]]=i;
for(i=0;i){
if(k) k--;
j
=sa[ra[i]-1];
while(A[i+k]==A[j+k]) k++;
h[ra[i]]
=k;
}
}
int check(int l,int n,int k){
int sum=1,ma=-1,i;
for(i=1;i<=n ;i++){
if(h[i]>=l){
sum
++;
if(masum;
}
else sum=1;
}
return ma>=k;
}
set<int>s;
set<int>::iterator it;
int main()
{
int i,n,k,m;
scanf(
"%d%d",&n,&k);
for(i=0;i)
{
scanf("%d",&A[i]);
s.insert(A[i]);
}
m
=0;
for(it=s.begin();it!=s.end();it++) ha[m++]= *it;
for(i=0;iha;
A[n]=-1;
build(n
+1,20005);
height(n);
int l=1,r=20005,mid;
while(l+1<r)
{
mid
=(l+r)/2;
if(check(mid,n,k)) l=mid ;
else r=mid;
}
if(check(l,n,k)) printf(" %d ",l);
else printf("0 ");
return 0;
}

Leave a Comment

Your email address will not be published.