codeforces B. Yet Another Palindrome Problem

codeforces B. Yet Another Palindrome Problem_第1张图片

题目

题意:

问是否有一个子序列满足至少有3个字符,并且是个回文数。

思路:

简单的一个二重循环, i i i从0开始 j j j i + 2 i+2 i+2开始这样就可以保证至少3个了,然后判断 s i s_i si s j s_j sj是否相等。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef vector<int> vec;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 5010;
int main() {
    int t;
    read(t);
    while (t--) {
        int n;
        int a[maxn] = {0}, cnt[maxn] = {0};
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        bool flag = false;
        for (int i = 1; i <= n; i++) {
            for (int j = i + 2; j <= n; j++) {
                if (a[i] == a[j]) {
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }
        if (flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

你可能感兴趣的:(codeforces)