Question Link
Question:
Give N number, n <= 100 W, and find a continuous subsequence with no duplicates in it. What is the maximum length of this subsequence?
Ideas:
Open an array pos. Pos [x] indicates the position where x appears. The array is initialized to-1.
Use the start variable to record the start point of the current enumeration sequence. The initial value is 0.
Then enumerate this sequence and record the positions of each number in sequence,
Assume that the current enumeration is to I. before recording this position, check whether POS [arr [I] is greater than or equal to start. If it is greater, note that this number has already appeared in [start, I-1], and write down the length of the Child sequence that meets the condition. Then enumerate the next subsequence: Start = POS [I] + 1. ThisAlgorithmThe total complexity is O (n)
Code:
/** Ultraviolet A 11572 unique snowflakes **/# include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long int64; const int INF = 0x3f3f3f3f; const int maxn = 1000010; int arr [maxn]; int POS [maxn]; int main () {int ncase, m, n; scanf ("% d ", & ncase); While (ncase --) {scanf ("% d", & N); int CNT = 0; For (INT I = 0; I <N; ++ I) scanf ("% d", & arr [I]); memset (Pos,-1, sizeof (POS); int start = 0; int Maxx = 0; arr [N] = arr [n-1]; for (INT I = 0; I <= N; ++ I) {If (Pos [arr [I]> = Start) {int TMP = I-start; Maxx = max (TMP, Maxx ); start = POS [arr [I] + 1; POS [arr [I] = I;} else {pos [arr [I] = I ;}} printf ("% d \ n", Maxx);} return 0 ;}