1. 程式人生 > 其它 >迴文子序列

迴文子序列

迴文子序列

AcWing 3697
https://www.acwing.com/problem/content/3700/
注意是子序列 不是子串

程式碼

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 5050;

int T,n;
int a[N];


int main()
{
    cin >> T;
    
    while (T--)
    {
        cin >> n;
        for (int i = 1; i <= n; i ++ )
        {
            cin >> a[i];
        }
        
        
        int flag = false;
        for (int i = 1; i <= n ; i ++ )
        {
            for (int j = n; j > i; j -- )
            {
                
                if (a[i] == a[j])
                {
                    if (j - i + 1 >= 3)
                    {
                        flag = true;
                        break;
                    }
                }

            }
        }
            
        if (flag) cout << "YES" << endl;
        else cout << "NO" << endl;;
            
    }
    return 0;
}