Description
Assume an integer sequence contains N elements whose value could only be one of {0, 1, -1}. There may exist a positive integer D which can make the sum between i-th element and (i+D)-th element be zero, where i is a certain integer between 1 and N-D. Your task is to find out the maximal D.

Input
First line contains a integer T indicate the number of cases. (T<=50)
In each test case, the first line is N descript above. (N<=50000)
Then follows N integers the sequence contains.

Output
Each case output one integer D in a line. If there is no such a D, just output -1.

Sample Input

2
3
1 0 -1
1
1

Sample Output

2
-1

很简单的hash

#include<stdio.h>
#include<stdlib.h>;
#include<string.h>
#define inf 100001
int Max(int a,int b){
	return a>b?a:b;
}
int Min(int a,int b){
	return a>b?b:a;
}
int max[100100],min[100100],res;
int main(){
	int i,j,n,T,t,tem;
	scanf("%d",&T);
	for(t=1;t<=T;t++){
		scanf("%d",&n);
		int sum=0;
		res=-1;
		for(i=0;i<=2*n;i++){
			max[i]=0;
			min[i]=inf;
		}
		max[0]=0,min[0]=0;
		for(i=1;i<=n;i++){
			scanf("%d",&tem);
			sum+=tem;
			if(sum<0){
				min[n-sum]=Min(min[n-sum],i);
				max[n-sum]=Max(max[n-sum],i);
				res=Max(res,max[n-sum]-min[n-sum]);
			}
			else{
				min[sum]=Min(min[sum],i);
				max[sum]=Max(max[sum],i);
				res=Max(res,max[sum]-min[sum]);
			}
		}
		if(res>1)
			printf("%d\n",res-1);
		else
			printf("-1\n");
	}
	return 0;
}