POJ 3261 Usaco 2006 December Gold Milk Patterns

Source: Internet
Author: User

Title: Give a string to find the longest substring (overlapping) that has occurred over K times.


Idea: Now get out the SA array and the height array, and then the maximum value of the smallest number in the interval of the height array for each length of K. Why are so many people two points? This is just a monotone queue sweep once on the line.


CODE:


#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <

Algorithm> #define MAX 1000010 using namespace std;
int len,k;
int S[max],val[max],sa[max];

int Rank[max],height[max]; inline bool Same (int x,int y,int l) {return val[x] = = Val[y] && ((x + l >= len && y + l >= len) ||
(x + L < len && y + L < len && val[x + l] = = Val[y + l]));
	} void Getsuffixarray () {static int _val[max],cnt[max],q[max],lim = 1000001;
	for (int i = 0; i < len; ++i) ++cnt[val[i] = s[i];
	for (int i = 1; i < Lim; ++i) cnt[i] + = cnt[i-1];
	for (int i = len-1; ~i; i.) sa[--cnt[val[i]] = i;
		for (int d = 1;; ++d) {int top = 0,l = 1 << (d-1);
		for (int i = 0; i < len; ++i) if (Sa[i] + l >= len) q[top++] = Sa[i];
		
		for (int i = 0; i < len; ++i) if (Sa[i] >= l) q[top++] = sa[i]-l;
		for (int i = 0; i < Lim; ++i) cnt[i] = 0;
		for (int i = 0; i < len; ++i) ++cnt[val[q[i]];for (int i = 1; i < Len; ++i) cnt[i] + = cnt[i-1];
		for (int i = len-1; ~i; i.) sa[--cnt[val[q[i]]] = q[i];
		Lim = 0;
			for (int i = 0,j, i < Len; ++lim) {for (j = i; j < len-1 && Same (sa[j],sa[j + 1],l); ++j);
		for (; I <= J; ++i) _val[sa[i] = Lim;
		} for (int i = 0; i < len; ++i) val[i] = _val[i];
	if (lim = = len) break;
} return;
	} void GetHeight () {for (int i = 0; i < len; ++i) rank[sa[i]] = i;
		for (int k = 0,i = 0; i < len; ++i) {if (k)--k;
		Int j = Sa[rank[i]-1];
		while (S[i + K] = = S[j + K]) ++k;
	Height[rank[i]] = k;
	
	}} struct complex{int pos,val;

Complex (int _,int __):p OS (_), Val (__) {} Complex () {}};
	int main () {cin >> len >> K;
	for (int i = 0; i < len; ++i) scanf ("%d", &s[i]);
	Getsuffixarray ();
	GetHeight ();
	Deque<complex> Q;
	int ans = 0;
		for (int i = 0; i < len, ++i) {while (!q.empty () && height[i] <= q.back (). val) Q.pop_back (); while (!q.empty () && i-q.Front (). POS >= k-1) Q.pop_front ();
		Q.push_back (Complex (i,height[i));
	ans = max (Ans,q.front (). val);
	} cout << ans << endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.