B - 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=an−i−1ai=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 (1≤t≤1001≤t≤100) — the number of test cases.

Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3≤n≤50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤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 (∑n≤5000∑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;
}

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

时间: 2024-10-11 00:41:11

B - Yet Another Palindrome Problem的相关文章

CF1324B Yet Another Palindrome Problem 题解

原题链接 CF 127个测试点,好评 简要题意: 多组数据,问数组中是否有长度 \(\geq 3\) 的回文子序列. 我们需要找到本质. 题目不让我们求这个长度,只让我们判断,这是为什么呢? 如果答案是 YES,那么必然存在一个长度为 \(3\) 的回文子序列.否则为 NO. 你想,如果原数组的最长回文序列是奇数的话,只要每次在两边同时删去一个,就可以得到长度为 \(3\) 的回文子序列. 如果是偶数,只要每次在两边同时删去一个,再在最中间两个任意删去一个,也可以得到长度为 \(3\) 的回文子

2016-2017 ACM-ICPC CHINA-Final Solution

Problem A. Number Theory Problem Solved. 水. 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1e5 + 10; 6 7 typedef long long ll; 8 9 int n; 10 ll arr[maxn], ans[maxn]; 11 12 void Init() 13 { 14 arr[0] = 1; 15 for(int i = 1; i

Codeforces Round #627 (Div. 3) 补题

CF1324A Yet Another Tetris Problem 长度为n的数组a中有一组数,可以任意使其中一项+2,问能否使a中所有项的值相同. 感觉div.3的题目更多地在考简化问题的能力--比如原题目以俄罗斯方块作背景,让我想到的是能不能消除所有方块,导致代码很难写.但如果像上述一样简化题意,方向就很明确了:只要判断是否所有数均为偶数/均为奇数即可. CF1324A-代码 #include<cstdio> #include<iostream> #include<cs

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could a

Problem 2902 - palindrome(最长公共字串)

Longest PalindromeTime Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatus Description Problem D: Longest Palindrome Time limit: 10 seconds A palindrome is a string that reads the same from the left as it does from the right.

Problem Palindrome Partitioning

Problem Description: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Solution: 1 public List<List<String>> partition(String s) { 2 List<List<Stri

Problem Valid Palindrome

Problem Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have

[LeetCode&amp;Python] Problem 409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the lengt

SPOJ Problem 5:The Next Palindrome

题目大意:根据给出的数字串求较大的最小回文串 简单题,可以从中间开始比较,如果左边大于右边的话则终止,若右边大于左边的话则中间+1,然后向左边推.输出时按左边复制一遍. #include<cstdio> #include<cstring> int t,i,j,l,r,n,pd,q,mid; char s[1000005]; int main(){ scanf("%d",&t); while(t--){ pd=q=l=r=0; scanf("%s