Acwing 799.最长连续不重复子序列

原题链接

代码

#include<iostream>
using namespace std;

const int N = 100010;
int a[N],f[N];

int main(){
    int n; cin >> n;
    
    int ans = 0, j = 1;
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);//读入该数组
        f[a[i]]++;//计数
        while(f[a[i]] > 1){//如果计数超过1说明已重复 让j往后走
            f[a[j]]--;//针对每一个,减去一,直到减掉重复的
            j++;
        }
        ans = max(ans, i - j + 1);//序列长度计算 i - j + 1
    }
        
    cout << ans;
    return 0;
}
posted @ 2023-04-02 09:22  天黑星更亮  阅读(13)  评论(0)    收藏  举报