B. Yet Another Palindrome Problem

B. Yet Another Palindrome Problem

题意

在一个字符串中,能否找到一个长度为3的回文子串。

思路

O(n^2)暴力枚举。

代码实现

#include<bits/stdc++.h>
using namespace std;
int main(void){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int a[n];
        for(int i = 1; i <= n; i++) cin >> a[i];
        bool flag = 1;
        for(int i = 1; i <= (n+1)/2; i++){
            if(a[i]!=a[n-i+1]){
                flag = 0;
            }
        } 
        if(flag) cout<<"YES"<<endl;
        else{
            bool flag = 0;
            for(int i = 1; i <= n; i++){
                for(int j = n; j > i+1; j--){
                    if(a[i]==a[j]){
                        flag = 1;
                        break;
                    }
                }
                if(flag) break;
            }
            if(flag) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AC-AC/p/12503115.html