Fourth week of training (efficient algorithm design) k problem (sliding window)

Source: Internet
Author: User

UVA 11572 the only snowflake

Test instructions: give you an array from 1 to N, ask for the longest consecutive non-repeating subsequence, classic sliding window problem, the method is to maintain a window, set the left and Right boxes, and then continue to maintain and update

Method One:

#include"Iostream"#include"Set"#include"CString"#include"Cstdio"#include"Algorithm"UsingNamespace Std;Constint MAXN=1000000+10; set<Int>book;int a[MAXN];IntMain(){int T; Cin>>t;While(T--){int nT, Sum=0, ans=0; Cin>>n; Book.Clear();For(int I=0; I<n; I++){cin>>a[I];}int R=0, l=0;For(; r<n;){While(!book.Count(A[R]) &&r<n){Book.insert (A[r]) ; R++;  Ans=max ( Ans,r-lerase (A[l++;  Cout<<ans<<endl return 0;< Span class= "Sh-cbracket" >}         

Method Two:

#include"Iostream"#include"Set"#include"CString"#include"Cstdio"#include"Algorithm"UsingNamespace Std;Constint MAXN=1000000+10;int POS[MAXN];int a[MAXN];IntMain(){int T; Cin>>t;While(T--){int nT, Sum=0, ans=0; Cin>>n;Memset(POS,-1,sizeof(POS));For(int I=0; I<n; I++){cin>>a[I];}int R=0, l=0; A[n]=a[n-1];For(int I=0; I<=n; I++){If(POS[A[I]]>=l {ans=max< Span class= "Sh-symbol" > (Ans,i-l=pos[a[i]]+1 Pos[a[i]]=i  Cout<<ans<<endl return 0;< Span class= "Sh-cbracket" >             

Fourth week of training (efficient algorithm design) k problem (sliding window)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.