首页 > 其他 > 详细

B - Yet Another Palindrome Problem

时间:2020-03-14 23:27:37      阅读:114      评论:0      收藏:0      [点我收藏+]

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

简单的回文字符串,反序求最长公共子序列,如果能大于三就是yes,感觉可以在dp内判断是否大于三,可以尝试一下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;



int main()
{
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        int a[5001] = { 0 };
        int b[5001] = { 0 };

        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            b[n - i - 1 ] = a[i];
        }


        int dp[5001][5001] = { 0 };
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (a[i] == b[j]) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                }
                else {
                    dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
                }
            }
        }

        if (dp[n][n] >= 3) {
            printf("YES\n");
        }
        else {
            printf("NO\n");
        }
    }




    return 0;
}

 

B - Yet Another Palindrome Problem

原文:https://www.cnblogs.com/Vetsama/p/12495006.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!