日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

P2852 [USACO06DEC]Milk Patterns G

發(fā)布時(shí)間:2023/12/3 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P2852 [USACO06DEC]Milk Patterns G 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

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.

農(nóng)夫John發(fā)現(xiàn)他的奶牛產(chǎn)奶的質(zhì)量一直在變動(dòng)。經(jīng)過(guò)細(xì)致的調(diào)查,他發(fā)現(xiàn):雖然他不能預(yù)見(jiàn)明天產(chǎn)奶的質(zhì)量,但連續(xù)的若干天的質(zhì)量有很多重疊。我們稱之為一個(gè)“模式”。 John的牛奶按質(zhì)量可以被賦予一個(gè)0到1000000之間的數(shù)。并且John記錄了N(1<=N<=20000)天的牛奶質(zhì)量值。他想知道最長(zhǎng)的出現(xiàn)了至少K(2<=K<=N)次的模式的長(zhǎng)度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出現(xiàn)了兩次。當(dāng)K=2時(shí),這個(gè)長(zhǎng)度為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復(fù)制

8 2 1 2 3 2 3 2 3 1

輸出 #1復(fù)制

4

題解:

題目自帶翻譯,愛(ài)了愛(ài)了
題意很明確了,其實(shí)就是求出現(xiàn)至少k次的子串的最大長(zhǎng)度
這是后綴數(shù)組Height的應(yīng)用
后綴數(shù)組這里就不介紹了講解鏈接
子串可以看做是后綴的前綴(想盡辦法和后綴扯上關(guān)系),出現(xiàn)k次的子串說(shuō)明至少有k個(gè)后綴的lcp是這個(gè)子串,我們對(duì)后綴排序,說(shuō)明至少有連續(xù)k個(gè)后綴的LCP是這個(gè)后綴,既然是連續(xù),那么我們只需要看頭和尾就行
所以,求出每相鄰k-1個(gè)height的最小值,然后求這些最小值的最大值就是我們要的答案,注意,k一開(kāi)始要減1,因?yàn)閔eight[i]是當(dāng)前第i個(gè)串和第i-1個(gè)串,涉及兩個(gè)串,所以k一開(kāi)始要減1
可以用單調(diào)隊(duì)列O(n)解決
這也算是后綴數(shù)組Height應(yīng)用的模板題,這幾天做了不少后綴數(shù)組的題,題目的難度評(píng)分都很高,基本上都是這種
但實(shí)際大部分都是套上模板就能做的,因?yàn)楸旧磉@個(gè)算法就比較難,所以背過(guò)模板會(huì)遷移就能結(jié)果后綴數(shù)組的大部分問(wèn)題

代碼:

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個(gè)后綴的排名; sa[i] 排名為i的后綴位置; height[i] 排名為i的后綴與排名為(i-1)的后綴的LCP //tax[i] 計(jì)數(shù)排序輔助數(shù)組; tp[i] rk的輔助數(shù)組(計(jì)數(shù)排序中的第二關(guān)鍵字),與sa意義一樣。 //a為原串 void RSort() {//rk第一關(guān)鍵字,tp第二關(guān)鍵字。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]; //確保滿足第一關(guān)鍵字的同時(shí),再滿足第二關(guān)鍵字的要求 } //計(jì)數(shù)排序,把新的二元組排序。int cmp(int* f, int x, int y, int w) {return f[x] == f[y] && f[x + w] == f[y + w]; } //通過(guò)二元組兩個(gè)下標(biāo)的比較,確定兩個(gè)子串是否相同 int maxx; void Suffix() {//safor (int i= 1; i <= n; i++)rk[i]= a[i], tp[i]= i;m= maxx, RSort(); //一開(kāi)始是以單個(gè)字符為單位,所以(m = 127)for (int w= 1, p= 1, i; p < n; w+= w, m= p) { //把子串長(zhǎng)度翻倍,更新rk//w 當(dāng)前一個(gè)子串的長(zhǎng)度; m 當(dāng)前離散后的排名種類數(shù)//當(dāng)前的tp(第二關(guān)鍵字)可直接由上一次的sa的得到for (p= 0, i= n - w + 1; i <= n; i++)tp[++p]= i; //長(zhǎng)度越界,第二關(guān)鍵字為0for (i= 1; i <= n; i++)if (sa[i] > w)tp[++p]= sa[i] - w;//更新sa值,并用tp暫時(shí)存下上一輪的rk(用于cmp比較)RSort(), swap(rk, tp), rk[sa[1]]= p= 1;//用已經(jīng)完成的sa來(lái)更新與它互逆的rk,并離散rkfor (i= 2; i <= n; i++)rk[sa[i]]= cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;}//離散:把相等的字符串的rk設(shè)為相同。//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);//這個(gè)知道原理后就比較好理解程序 } 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 */

總結(jié)

以上是生活随笔為你收集整理的P2852 [USACO06DEC]Milk Patterns G的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。