B - a simple method of Yet Another Palindrome Problem

You are given an array aa consisting of nn integers.

Your task is to determine if aa has some subsequence of length at least 33 that is a palindrome.

Recall that an array bb is called a subsequence of the array aa if bb can be obtained by removing some (possibly, zero) elements from aa (not necessarily consecutive) without changing the order of remaining elements. For example, [2][2], [1,2,1,3][1,2,1,3] and [2,3][2,3] are subsequences of [1,2,1,3][1,2,1,3], but [1,1,2][1,1,2]and [4][4] are not.

Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array aa of length nn is the palindrome if ai=ani1ai=an−i−1 for all ii from 11 to nn. For example, arrays [1234][1234], [1,2,1][1,2,1], [1,3,2,2,3,1][1,3,2,2,3,1] and [10,100,10][10,100,10] are palindromes, but arrays [1,2][1,2] and [1,2,3,1][1,2,3,1] are not.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t1001≤t≤100) — the number of test cases.

Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3n50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n), where aiai is the ii-th element of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000 (n5000∑n≤5000).

Output

For each test case, print the answer — "YES" (without quotes) if aa has some subsequence of length at least 33 that is a palindrome and "NO" otherwise.

Example

Input
5
3
1 2 1
5
1 2 2 3 2
3
1 1 2
4
1 2 2 1
10
1 1 2 2 3 3 4 4 5 5
Output
YES
YES
NO
YES
NO

Note

In the first test case of the example, the array aa has a subsequence [1,2,1][1,2,1] which is a palindrome.

In the second test case of the example, the array aa has two subsequences of length 33 which are palindromes: [2,3,2][2,3,2] and [2,2,2][2,2,2].

In the third test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

In the fourth test case of the example, the array aa has one subsequence of length 44 which is a palindrome: [1,2,2,1][1,2,2,1] (and has two subsequences of length 33 which are palindromes: both are [1,2,1][1,2,1]).

In the fifth test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.

This means that the problem can not find a palindrome in a set of numbers (as long as there are three numbers on the line, and the location in the array need not be next)

This question is then further simplified idea is to find whether the two equal numbers in an array, and at least a spacer between the first position number and the second number Off

Upper bound of the questions asked of the array size is 5000, must be defined outside the main function of this array, because the array is too big , then when the game could not find where the wrong, to know because of the defined in the global down after

#include<iostream>
using namespace std;
const long long maxn=5000+10;
int a[maxn];
int main(){
    int t,i,j,flag,k,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        flag= 0 ;
         for (I = 0 ; I <N- 2 ; I ++ ) {// iterate first number up to the third position of the inverted
             IF (I & 2 == 0 ) Continue ; // from the zero position If the position of the first number is a multiple of 2, and then the location is a location on the second number coincides skip
                 for (I + K = 2 ; K <n-; K ++ ) {
                     IF (a [K] == A [I]) { 
                        COUT << " YES " << endl; 
                        In Flag = . 1 ;
                         BREAK  ;
                    }
                }
                if(flag==1) break;
        }
        if(i==n-2) cout<<"NO"<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/ZJK132/p/12509936.html