B. Yet Another Palindrome Problem
标签
简明题意
- 给一个序列,问是否存在长度>=3的子序列且为回文。
思路
- 对于每一个a[i],从j=i+2开始找如果有找到某个a[j]=a[i],就yes。这种复杂度n方。而题目的n是5000,所以可以过。
- 讲讲O(n)的算法。开一个
vector<int> g[maxn]
,然后在每个vector里暴力找。
注意事项
- 无
总结
- 无
AC代码
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<set>
#include<map>
#include<string>
using namespace std;
void solve()
{
int t;
cin >> t;
while (t--)
{
vector<int> g[5000 + 10];
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;
g[t].push_back(i);
}
bool ok = 0;
for (int i = 1; i <= 5000; i++)
{
int las = -1;
for (auto& it : g[i])
if (las == -1) las = it;
else if (it - las >= 2)
{
ok = 1;
break;
}
if (ok)
{
cout << "YES" << endl;
break;
}
}
if (!ok) cout << "NO" << endl;
}
}
int main()
{
//freopen("Testin.txt", "r", stdin);
solve();
return 0;
}