POJ1743:Musical Theme

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1743:Musical Theme相关的知识,希望对你有一定的参考价值。

题目

vjudge

Sol

先差分
然后求不可重叠最长重复子串

bits/stdc++.h会CE

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(20010);

IL ll Read(){
	RG char c = getchar(); RG ll x = 0, z = 1;
	for(; c < \'0\' || c > \'9\'; c = getchar()) z = c == \'-\' ? -1 : 1;
	for(; c >= \'0\' && c <= \'9\'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * z;
}

int n, a[_], sa[_], rk[_], y[_], h[_], height[_], t[_], ans;

IL bool Cmp(RG int i, RG int j, RG int k){  return y[i] == y[j] && y[i + k] == y[j + k];  }

IL void Sort(){
	RG int m = 180;
	for(RG int i = 1; i <= m; ++i) t[i] = 0;
	for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
	for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
	for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
	for(RG int k = 1; k <= n; k <<= 1){
		RG int l = 0;
		for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
		for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
		for(RG int i = 1; i <= m; ++i) t[i] = 0;
		for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
		for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
		for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
		swap(rk, y); rk[sa[1]] = l = 1;
		for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i], sa[i - 1], k) ? l : ++l;
		if(l >= n) break; m = l;
	}
	for(RG int i = 1; i <= n; ++i){
		h[i] = max(0, h[i - 1] - 1);
		if(rk[i] == 1) continue;
		while(a[i + h[i]] == a[sa[rk[i] - 1] + h[i]]) ++h[i];
	}
	for(RG int i = 1; i <= n; ++i) height[i] = h[sa[i]];
}

IL bool Check(RG int k){
	RG int mn = sa[1], mx = sa[1];
	for(RG int i = 2; i <= n; ++i){
		if(height[i] >= k) mx = max(mx, sa[i]), mn = min(mn, sa[i]);
		else mx = mn = sa[i];
		if(mx - mn > k) return 1;
	}
	return 0;
}

int main(RG int argc, RG char* argv[]){
	for(n = Read(); n; n = Read()){
		Fill(a, 0); ans = 0;
		for(RG int i = 1; i <= n; ++i) a[i] = Read();
		for(RG int i = 1; i <= n; ++i) a[i] = a[i + 1] - a[i] + 88;
		Sort();
		RG int l = 4, r = n;
		while(l <= r){
			RG int mid = (l + r) >> 1;
			if(Check(mid)) ans = mid + 1, l = mid + 1;
			else r = mid - 1;
		}
		printf("%d\\n", ans);
	}
	return 0;
}

以上是关于POJ1743:Musical Theme的主要内容,如果未能解决你的问题,请参考以下文章

poj1743 Musical Theme

POJ-1743 Musical Theme(后缀数组)

poj 1743 Musical Theme

POJ 1743 Musical Theme ——后缀数组

POJ 1743. Musical Theme

POJ 1743-Musical Theme-后缀数组