AcWing 3697. 回文子序列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
using namespace std;
const int N = 5001;
int T;
int n;
int p[N];
int x;
bool ans;
int main() {
cin >> T;
while (T--) {
ans = false;
cin >> n;
memset(p, 0, sizeof p);
for (int i = 1; i <= n; ++i) {
cin >> x;
if (p[x]) {
if (i - p[x] > 1) {
ans = true;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
} else {
p[x] = i;
}
}
cout << (ans ? "YES" : "NO") << endl;
}
return 0;
}