uva 11572 Unique Snowflakes (唯一的雪花)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PR_sc/article/details/77115055

题目链接

题意输入一个长度为n 的序列,找到一个尽量长的连续子序列Al~Ar,使得该序列中没有相同元素。
分析 用左右端点去遍历 然后用set查重。

#include<bits/stdc++.h>
using namespace std;
int a[1000005],len,l,r,t,n;
int main (void){
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=0;i<n;i++) cin>>a[i];
        set<int>s;
        l=0;r=0;len=0;
        while(r<n){
            while( r<n && !s.count(a[r])) s.insert(a[r++]);
            len=max(len,r-l);
            s.erase(a[l++]);
        }
        cout<<len<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PR_sc/article/details/77115055