Codeforce 1324B—— Yet Another Palindrome Problem (子列迴文)

Codeforce 1324B Yet Another Palindrome Problem

題目

在這裏插入圖片描述
在這裏插入圖片描述

題目大意:
給你一串數,他含有元素大於等於3的子列如果是迴文的那麼就輸出YES

題目分析:
只要原來的數列中,有不相鄰的兩個元素相等就可以

AC代碼

//https://codeforces.com/problemset/problem/1324/B 
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int a[5005],t,n,ans=0;


int main(){
	scanf("%d",&t);
	while(t--){
		int n,ans=0;
		scanf("%d",&n);
		for(int i = 0;i<n;i++)
			scanf("%d",&a[i]); 
		for (int i=0;i<n;i++){
			for(int j = i+2;j<n;j++){
				if(a[i]==a[j]){
					ans = 1;
					break;
				}
			}
			if(ans)
			break;//找到一個就可以了 
		}
		if(ans)
		printf("YES\n");
		else
		printf("NO\n");
		
		
	}
	
	
}

再看看大佬們的優質代碼

在這裏插入圖片描述大佬的博客在這裏
比起我這暴力掃,大佬的結構體和卡區間的方法更爲方便。並且對於邊界的處理,再a[0]和a[n+1]的位置設置永遠可能和數列中數相等的數,十分巧妙。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章