您的位置:首页 > 其它

POJ 3261 Milk Patterns 及其变形(后缀数组)

2013-03-15 20:27 337 查看
Milk Patterns

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 7337Accepted: 3331
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
思路:可重叠的k次最长重复子串,论文上有,注意这道题的between 0 and 1,000,000 inclusive,10^6,


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;

const int maxn=1010000,maxl=maxn-2000,maxm=1010000;
int wss[maxn],wa[maxn],wb[maxn],wv[maxn];
int r[maxm],sa[maxm],rank[maxm],h[maxm];
int n,m,t,maxx,minn,he,mid,kk,x;

void close()
{
}

int cmp(int *r,int x,int y,int l)
{
return (r[x]==r[y] && r[x+l]==r[y+l]);
}

void da(int n,int m,int *ws)
{
int i,j,*t,*x=wa,*y=wb,p;
for (i=0;i<m;i++) ws[i]=0;
for (i=0;i<n;i++) ws[x[i]=r[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for (j=1,p=1;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++) ws[i]=0;
for (i=0;i<n;i++) ws[wv[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
}
}

void callheight(int n)
{
int i,j,k=0;
for (i=1;i<=n;i++) rank[sa[i]]=i;
for (i=0;i<n;h[rank[i++]]=k)
for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}

bool check(int k)
{
minn=sa[1];maxx=sa[1];
int cnt=0;
for (int i=2;i<=n;i++)
{
if (h[i]>=k)
{
cnt++;
if (cnt+1>=kk)
return true;
}
else
{
cnt=0;
}
}
return false;
}

void work()
{
he=0;t=n;
while (he<=t)
{
//        printf("he:%d mid:%d t:%d\n",he,mid,t);
mid=(he+t)>>1;
if (check(mid))
he=mid+1;
else t=mid-1;
}
printf("%d\n",t);
}

void init ()
{
while(scanf("%d %d",&n,&kk)!=EOF)
{
for (int i=0;i<n;i++)
{
scanf("%d",&x);
r[i]=x+97;
}
r
=0;
da(n+1,maxl,wss);
callheight(n);
work();
}
}

int main ()
{
init();
close();
return 0;
}


接下来的一道题

Time Limit:10000msMemory Limit:262144kBDescription
给一个字符集很大的字符串。用数字表示字符。求出其中至少重复K(2 ≤ K ≤ N)次的最长子串(可重叠)。例如在“1 2 3 2 3 2 3 1”中“2 3 2 3”重复了二次。

Input第一行:二个整数,N和K。(N<10^5,2 ≤ K ≤ N)
第2...N+1行:每行一个整数C。(0≤C≤10^9)Output一个整数,重复了至少K次的子串的长度。Sample Input
8 2
1
2
3
2
3
2
3
1

Sample Output
4
思路:m的值达到了10^9,会RT的,所以需要先对原数组排一道序,所以我用了map。。。。。


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;

const int maxn=100100,maxm=maxn;
int wss[maxn],wa[maxn],wb[maxn],wv[maxn];
int r[maxm],sa[maxm],rank[maxm],h[maxm];
int n,m,t,maxx,minn,he,mid,kk,x,a[maxn];

map<int,int> mp;

void close()
{
}

int cmp(int *r,int x,int y,int l)
{
return (r[x]==r[y] && r[x+l]==r[y+l]);
}

void da(int n,int m,int *ws)
{
int i,j,*t,*x=wa,*y=wb,p;
for (i=0;i<m;i++) ws[i]=0;
for (i=0;i<n;i++) ws[x[i]=r[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for (j=1,p=1;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++) ws[i]=0;
for (i=0;i<n;i++) ws[wv[i]]++;
for (i=1;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
}
}

void callheight(int n)
{
int i,j,k=0;
for (i=1;i<=n;i++) rank[sa[i]]=i;
for (i=0;i<n;h[rank[i++]]=k)
for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}

bool check(int k)
{
minn=sa[1];maxx=sa[1];
int cnt=0;
for (int i=2;i<=n;i++)
{
if (h[i]>=k)
{
cnt++;
if (cnt+1>=kk)
return true;
}
else
{
cnt=0;
}
}
return false;
}

void work()
{
he=0;t=n;
while (he<=t)
{
//        printf("he:%d mid:%d t:%d\n",he,mid,t);
mid=(he+t)>>1;
if (check(mid))
he=mid+1;
else t=mid-1;
}
printf("%d\n",t);
}

void init ()
{
while(scanf("%d %d",&n,&kk)!=EOF)
{
for (int i=0;i<n;i++)
{
scanf("%d",&x);
r[i]=x+97;
a[i]=r[i];
}
sort(a,a+n);
for (int i=0;i<n;i++)
mp[a[i]]=i;
for (int i=0;i<n;i++)
{
r[i]=mp[r[i]]+3;
}
r
=0;
da(n+1,maxn,wss);
callheight(n);
work();
}
}

int main ()
{
init();
close();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: