B. Yet Another Palindrome Problem
题目链接-B. Yet Another Palindrome Problem
题目大意
给一个长为n(≤5000)的数组,问是否存在一个长度至少为3的子序列是回文的,子序列的数可以不连续但是相对顺序不可变
解题思路
暴力,因为可以不连续,只要找有两位相等的而且不相邻的数即可
附上代码
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
int a[5050];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
bool flag=0;
for(int i=0;i<n-2;i++){
for(int j=i+2;j<n;j++){
if(a[i]==a[j]){
flag=1;
break;
}
}
if(flag)
break;
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}