回文子序列

回文子序列

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;
}

回文子序列

上一篇:KMP子串匹配(只能匹配出唯一子串)


下一篇:Measures of Speed and Capacity(一些计量单位)