码迷,mamicode.com
首页 > 其他好文 > 详细

最长连续不重复子序列

时间:2020-03-25 00:50:16      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:max   没有   target   code   problem   win   题解   pac   get   

# 题意

给定长度为n 的序列,求连续的没有重复元素的最长子序列的长度

# 题解
双指针,保证j<i ,用一个数组记录当前值的出现个数,
因为是连续的,当 a[ i ]出现两次的时候,j一直向前,并且映射的次数减1,直到 i 对应的值的出现次数为1时候,取最大记录即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 int n;
 5 int a[N],s[N];
 6 int main(){
 7     ios::sync_with_stdio(0);
 8     cin.tie(0);
 9     cout.tie(0);
10     cin>>n;
11     for(int i=0;i<n;i++)
12         cin>>a[i];
13 
14     int res=0;
15     for(int i=0,j=0;i<n;i++) {
16         s[ a[i] ]++;
17         while(j<i&&s[a[i]]>1) {
18             s[a[j]]--;
19             j++;
20         }
21         res=max(res,i-j+1);
22     }
23     cout<<res;
24 }

 

最长连续不重复子序列

标签:max   没有   target   code   problem   win   题解   pac   get   

原文地址:https://www.cnblogs.com/hhyx/p/12563479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!