题目描述
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.
农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。
输入格式
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.
输出格式
Line 1: One integer, the length of the longest pattern which occurs at least K times
输入输出样例
输入 #1复制
8 2
1
2
3
2
3
2
3
1
输出 #1复制
4
题解:
题目自带翻译,爱了爱了
题意很明确了,其实就是求出现至少k次的子串的最大长度
这是后缀数组Height的应用
后缀数组这里就不介绍了讲解链接
子串可以看做是后缀的前缀(想尽办法和后缀扯上关系),出现k次的子串说明至少有k个后缀的lcp是这个子串,我们对后缀排序,说明至少有连续k个后缀的LCP是这个后缀,既然是连续,那么我们只需要看头和尾就行
所以,求出每相邻k-1个height的最小值,然后求这些最小值的最大值就是我们要的答案,注意,k一开始要减1,因为height[i]是当前第i个串和第i-1个串,涉及两个串,所以k一开始要减1
可以用单调队列O(n)解决
这也算是后缀数组Height应用的模板题,这几天做了不少后缀数组的题,题目的难度评分都很高,基本上都是这种
但实际大部分都是套上模板就能做的,因为本身这个算法就比较难,所以背过模板会迁移就能结果后缀数组的大部分问题
代码:
oi wiki的模板
// Problem: P2852 [USACO06DEC]Milk Patterns G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2852
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Data:2021-08-22 14:25:39
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int MAXN= 1000005;char ch[MAXN], all[MAXN];
int sa[MAXN], rk[MAXN], height[MAXN], tax[MAXN], tp[MAXN], a[MAXN], n, m;
char str[MAXN];
//rk[i] 第i个后缀的排名; sa[i] 排名为i的后缀位置; height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP
//tax[i] 计数排序辅助数组; tp[i] rk的辅助数组(计数排序中的第二关键字),与sa意义一样。
//a为原串
void RSort()
{//rk第一关键字,tp第二关键字。for (int i= 0; i <= m; i++)tax[i]= 0;for (int i= 1; i <= n; i++)tax[rk[tp[i]]]++;for (int i= 1; i <= m; i++)tax[i]+= tax[i - 1];for (int i= n; i >= 1; i--)sa[tax[rk[tp[i]]]--]= tp[i]; //确保满足第一关键字的同时,再满足第二关键字的要求
} //计数排序,把新的二元组排序。int cmp(int* f, int x, int y, int w)
{return f[x] == f[y] && f[x + w] == f[y + w];
}
//通过二元组两个下标的比较,确定两个子串是否相同
int maxx;
void Suffix()
{//safor (int i= 1; i <= n; i++)rk[i]= a[i], tp[i]= i;m= maxx, RSort(); //一开始是以单个字符为单位,所以(m = 127)for (int w= 1, p= 1, i; p < n; w+= w, m= p) { //把子串长度翻倍,更新rk//w 当前一个子串的长度; m 当前离散后的排名种类数//当前的tp(第二关键字)可直接由上一次的sa的得到for (p= 0, i= n - w + 1; i <= n; i++)tp[++p]= i; //长度越界,第二关键字为0for (i= 1; i <= n; i++)if (sa[i] > w)tp[++p]= sa[i] - w;//更新sa值,并用tp暂时存下上一轮的rk(用于cmp比较)RSort(), swap(rk, tp), rk[sa[1]]= p= 1;//用已经完成的sa来更新与它互逆的rk,并离散rkfor (i= 2; i <= n; i++)rk[sa[i]]= cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;}//离散:把相等的字符串的rk设为相同。//LCPint j, k= 0;for (int i= 1; i <= n; height[rk[i++]]= k)for (k= k ? k - 1 : k, j= sa[rk[i] - 1]; a[i + k] == a[j + k]; ++k);//这个知道原理后就比较好理解程序
}
int k;
void Init()
{read(n, k);// for (int i= 1; i <= n; i++)// printf("%c", str[i]);for (int i= 1; i <= n; i++)cin >> a[i], maxx= max(maxx, a[i]);
}
int main()
{//rd_test();Init();Suffix();int ans= 0;deque<int> q;k--;// for (int i= 1; i <= n; i++)// cout << "hegiht[i]=" << height[i] << endl;for (int i= 1; i <= n; i++) {while (!q.empty() && q.front() + k <= i)q.pop_front();while (!q.empty() && height[q.back()] >= height[i])q.pop_back();q.push_back(i);if (i >= k)ans= max(ans, height[q.front()]);}cout << ans;//Time_test();
}/*
12323231
*/