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