题目大意:求给定序列的最大连续的各个数字不同的序列长度。
解题:此题不是难题,就是使用一个hash表来查找在前面是否存在一个数与当前的相等,如果相等就将左边的边界设为相等的数的pos+1,再将当前的数加入hash表中,唯一注意的点就是在hash查找的时候注意判断左边的下界就行了。
//
// main.cpp
// uva 11572 - Unique Snowflakes
//
// Created by XD on 15/8/13.
// Copyright (c) 2015年 XD. All rights reserved.
//
//窗口滑动 + hash查找
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;
int n ;
int lowerbound ;
const int hashsize = 1000003 ;
int st[hashsize] ;
int head[hashsize] ; int mnext[hashsize] ;
int max(int x , int y)
{
return x > y ? x :y ;
}
void init()
{
memset(head, 0, sizeof(head)) ;
memset(mnext, 0, sizeof(int ) * (n + 5)) ;
}
int HASH(int pos)
{
return st[pos] % hashsize ;
}
int insert_into_table(int pos)
{
int flag = 1 ;
int h = HASH(pos) ;
int u = head[h] ;
while (u!=0) {
if(u >= lowerbound && st[u] == st[pos])
{
lowerbound = u + 1 ;
// mnext[pos] = head[h] ;
// head[h] = pos ;
flag = 0 ;
break ;
}
u = mnext[u] ;
}
mnext[pos] = head[h] ;
head[h] = pos ;
return flag;
}
int main() {
int casenum ;
int d ;
scanf("%d",&casenum) ;
while (casenum--) {
int ans = 0 ;
int t = 0 ;
lowerbound = 1 ;
init() ;
scanf("%d" ,&n) ;
for (int i =1 ; i <=n ; i++) {
scanf("%d" ,&d) ;
st[i] = d ;
if(insert_into_table(i))
{
t++ ;
}
else{
t = i -lowerbound + 1 ;
}
ans = max(ans ,t) ;
}
printf("%d\n",ans) ;
}
return 0;
}