您的位置:首页 > 其它

POJ 3261:Milk Patterns

2016-02-01 23:47 393 查看
Milk Patterns

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 12821Accepted: 5699
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次的最长重复子串。

还是二分这个答案x。对height值进行分组,如果某一组里面的height值都大于等于x,且至少有k个,返回true。

代码:

#pragma warning(disable:4996)  
#include <iostream>  
#include <algorithm>
#include <cstring>
#include <cstring>
#include <vector>  
#include <string>  
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffff
const int mod = 1e9 + 7;
const int maxn = 1000002;

int nx, kx;
int sa[maxn], num[maxn];
int ran[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], wss[maxn];

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

void da(int *r, int n, int m)            //  倍增算法0(nlgn)。
{
	int i, j, p, *x = wa, *y = wb, *t;
	for (i = 0; i < m; i++)
	{
		wss[i] = 0;
	}
	for (i = 0; i < n; i++)
	{
		wss[x[i] = r[i]] ++;
	}
	for (i = 1; i < m; i++)
	{
		wss[i] += wss[i - 1];
	}
	for (i = n - 1; i >= 0; i--)
	{
		sa[--wss[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++)//sa[x]=y 表示排第x的是y位置
		{
			if (sa[i] >= j)//假设sa[i]=x,便表示排第i位的是第x位置,
						   //要满足x>=j,第二关键字赋值才有意义
						   //如果满足了这个关系,即在y上累加,做记录(前面比它小的都做完记录了)
			{
				y[p++] = sa[i] - j;
			}
		}
		for (i = 0; i < n; i++)
		{
			wv[i] = x[y[i]];//y[i]=x 表示第二关键字排第i位的是x
							//x数组保存的就是当前的rank值
							//x[i]=j 表示i后缀 排到了第j位
							//x[y[i]]的意思就是把第二关键字排第i位
							//的那个第一关键字搞出来,按照第一关键字排序
							//相等的第一关键字 按照第二关键字排序
		}
		for (i = 0; i < m; i++)
		{
			wss[i] = 0;
		}
		for (i = 0; i < n; i++)
		{
			wss[wv[i]] ++;
		}
		for (i = 1; i < m; i++)
		{
			wss[i] += wss[i - 1];
		}
		for (i = n - 1; i >= 0; i--)
		{
			sa[--wss[wv[i]]] = y[i];//y[i]=x 表示第二关键字排第i位的是x
									//sa[x]=y表示总体记录,整个的字符串排在第x位的是y
		}
		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 - 1], sa[i], j) ? p - 1 : p++;
		}
	}
}

void calHeight(int *r, int n)            //  求height数组
{
	int i, j, k = 0;
	for (i = 1; i <= n; i++)
	{
		ran[sa[i]] = i;
	}

	for (i = 0; i < n; i++)
	{
		if (k)
		{
			k--;
		}
		else
		{
			k = 0;
		}
		j = sa[ran[i] - 1];//表示后缀i排的位置前一位的 位置
		while (r[i + k] == r[j + k])
		{
			k++;
		}
		height[ran[i]] = k;
	}
}

bool check(int x)
{
	int i, k = 0;
	for (i = 1; i <= nx; i++)
	{
		if (k == 0)
		{
			if (height[i] >= x)
			{
				k = k + 2;
				if (k>= kx)
					return true;
			}
		}
		else
		{
			if (height[i] >= x)
			{
				k++;
				if (k >= kx)
					return true;
			}
			else
			{
				k = 0;
			}
		}
	}
	return false;
}

void input()
{
	int i;
	for (i = 0; i < nx; i++)
	{
		scanf("%d", num + i);
	}
}

void solve()
{
	num[nx] = 0;

	da(num, nx + 1, 1000000);
	calHeight(num, nx);

	int le, ri, mid;
	le = 0;
	ri = nx;

	while (le < ri)
	{
		mid = (le + ri + 1) / 2;
		if (check(mid))
		{
			le = mid;
		}
		else
		{
			ri = mid - 1;
		}
	}
	printf("%d\n", le);
}

int main()
{
	//freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);

	while (scanf("%d%d", &nx, &kx) != EOF)
	{
		input();
		solve();
	}

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