POJ 3261 Milk Patterns sa+二分

                    Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11944   Accepted: 5302
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 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

Source

 
 
 
 
题意:
给定一个"字符串",长度N<=20000
输入k
求满足出现k次或以上这个条件的所有子串中(可以重复),最长的子串的长度。
 
 
后缀数组+二分
复杂度:O(nlogn)
 
求出height数组后,由于最长子串的长度的范围为1~N
则二分长度
对于每一长度mid,对height数组进行分组
使得每一组内要不只有一个元素,要不该组内的height的值都>=mid,即该组内的LCP的长度>=mid
若存在一个组,该组内的元素个数>=k
说明答案至少为mid
继续二分mid~right
否则继续二分left~mid
 
 
47ms
 
POJ 3261 Milk Patterns sa+二分
  1 #include<cstdio>

  2 #include<cstring>

  3 #include<algorithm>

  4 

  5 using namespace std;

  6 

  7 const int MAXN=20000+5;

  8 const int MAXM=1000000+5;

  9 

 10 int c[MAXM];

 11 int t1[MAXN];

 12 int t2[MAXN];

 13 int sa[MAXN];

 14 int Rank[MAXN];

 15 int height[MAXN];

 16 int s[MAXN];

 17 

 18 void build_sa(int N,int m)

 19 {

 20     int i,*x=t1,*y=t2;

 21     for(i=0;i<m;i++)

 22         c[i]=0;

 23     for(i=0;i<N;i++)

 24         c[x[i]=s[i]]++;

 25     for(i=1;i<m;i++)

 26         c[i]+=c[i-1];

 27     for(i=N-1;i>=0;i--)

 28         sa[--c[x[i]]]=i;

 29 

 30     for(int k=1;k<=N;k<<=1)

 31     {

 32         int p=0;

 33         for(i=N-k;i<N;i++)

 34             y[p++]=i;

 35         for(i=0;i<N;i++)

 36             if(sa[i]>=k)

 37                 y[p++]=sa[i]-k;

 38 

 39         for(i=0;i<m;i++)

 40             c[i]=0;

 41         for(i=0;i<N;i++)

 42             c[x[y[i]]]++;

 43         for(i=1;i<m;i++)

 44             c[i]+=c[i-1];

 45         for(i=N-1;i>=0;i--)

 46             sa[--c[x[y[i]]]]=y[i];

 47 

 48         swap(x,y);

 49 

 50         p=1;

 51         x[sa[0]]=0;

 52         for(i=1;i<N;i++)

 53         {

 54             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;

 55         }

 56         if(p>=N)

 57             break;

 58         m=p;

 59     }

 60 }

 61 

 62 void getHeight(int N)

 63 {

 64     int i,k=0;

 65     for(i=1;i<=N;i++)

 66         Rank[sa[i]]=i;

 67     for(i=0;i<N;i++)

 68     {

 69         if(k)

 70             k--;

 71         int j=sa[Rank[i]-1];

 72         while(s[i+k]==s[j+k])

 73             k++;

 74         height[Rank[i]]=k;

 75     }

 76 }

 77 

 78 bool valid(int len,int N,int K)

 79 {

 80     int i=1;

 81     while(true)

 82     {

 83         while(i<=N&&height[i]<len)

 84             i++;

 85         if(i>N)

 86             return false;

 87         int cnt=0;

 88         while(i<=N&&height[i]>=len)

 89         {

 90             i++;

 91             cnt++;

 92         }

 93         if(cnt+1>=K)

 94             return true;

 95     }

 96 }

 97 

 98 int solve(int N,int K)

 99 {

100     int lb=1;

101     int rb=N;

102     while(rb-lb>1)

103     {

104         int mid=(rb+lb)>>1;

105         if(valid(mid,N,K))

106             lb=mid;

107         else

108             rb=mid;

109     }

110     return lb;

111 }

112 int main()

113 {

114     int N,K;

115     while(~scanf("%d%d",&N,&K))

116     {

117         int m=0;

118         for(int i=0;i<N;i++)

119         {

120             scanf("%d",&s[i]);

121             s[i]++;

122             if(s[i]>m)

123                 m=s[i];

124         }

125         m++;

126         s[N]=0;

127 

128         build_sa(N+1,m);

129         getHeight(N);

130 

131         int ans=solve(N,K);

132 

133         printf("%d\n",ans);

134 

135         /*

136         for(int i=0;i<N+1;i++)

137             printf("%d ",sa[i]);

138         printf("\n");

139         for(int i=0;i<N;i++)

140             printf("%d ",Rank[i]);

141         printf("\n");

142         for(int i=0;i<N;i++)

143             printf("%d ",height[i]);

144         */

145     }

146     return 0;

147 }
View Code

 

 

 

 

 

 

 
 
 
 
 
 
 
 

你可能感兴趣的:(Pattern)