joj 2557: Unique Snowflakes

[url]http://acm.jlu.edu.cn/joj/showproblem.php?pid=2557[/url]
这道题其实是求一个数字序列中连续没有重复的子序列的最大长度。
使用一个map来查是否已经出现,使用list来记录当前的序列,
每次遇到重复把重复数字之前的序列删掉,并更新当前的最长无重复序列的长度。


#include<iostream>
#include<map>
#include<list>
using namespace std;

int main(){
int ncase;
cin >> ncase;
map<int,int> mp;
list<int> qu;

while(ncase--){
int n;
cin >> n;
int i = 0;
mp.clear();
qu.clear();
int id;
int max = 0;
int current = 0;
for(; i < n; i++){
cin >> id;
if(mp.count(id) != 0){
if(max < qu.size()){
max = qu.size();
}
current = qu.front();
while(current != id){
qu.pop_front();
mp.erase(current);//Note: the mp don't erase the id element
current = qu.front();
}
qu.pop_front();//pop the element equals id
qu.push_back(id);//push the id
}else{
qu.push_back(id);
mp[id] = id;
}
}

cout << (max > qu.size() ? max : qu.size()) << endl;
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值