Problem Statement:
UVa 11572 - Unique Snowflakes
Summary:
Find the maximum |i−j| such that for a sequence a1,a2,…,an, the subsequence ai,ai+1,…,aj has distinct elements.
I like this problem, because it allows you to think a bit and reward you with happiness and joy (haha wth I'm writing that for). Strategy: Suppose we already have a subsequence T=ai,ai+1,…,aj. Upon deciding whether aj+1 can be added to this subsequence, we need a quick way to check whether the previous occurrence of v=aj+1 is before index i. Otherwise it's already in the subsequence, and therefore if there is a longer subsequence other than T, it must start after the occurrence of v in T (Proof by contradiction). By the end of this procedure, we will obtain the longest subsequence possible such that each element are all distinct.
There are various ways to get a fast look up for previous occurrences of an element: If ai are small, a simple array for direct addressing table (DAT) suffices. Otherwise, a good hash table with expected O(1) or a map with O(log N) look-up time will do.
No comments:
Post a Comment