给你一个序列A
要求只删除一个值(删除他的一部分或者全部或者不删)
使得原序列为回文序列
4 2 1 2 4
1 2 1
特别的
单个值也是回文序列
1
第二次碰到这种题目了,还是Wa了一发。
直接两个指针一头一尾。
碰到不同的时候就是不是你死就是我亡。
肯定是选择删除两个种的一个。
枚举即可。
#include//#include //priority_queue #define PII pair #define ll long long using namespace std; const int INF = 0x3f3f3f3f; const int N = 200100 ; int a[N] ; int n ; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int T ; cin >> T ; while (T--) { cin >> n ; for (int i = 1 ; i <= n ; i++ ) cin >> a[i] ; if ( n <= 2 ) cout << "YESn"; else { int t1 = 1 , t2 = n ; bool falg = 1 ; int ck = -1 ; while (t1 < t2 ) { if ( a[t1] != a[t2] && ck == -1 ) { ck = a[t1] ; falg = 1 ; int s1 = t1 , s2 = t2 ; while ( t1 < t2 ) { if ( a[t1] != a[t2] && ck != -1 ) { if ( a[t1] == ck ) t1++; else if (a[t2] == ck ) t2--; else { falg = 0 ; break ; } } if (a[t1] == a[t2] ) { t1++; t2--; } } if (falg) break ; t1 = s1 , t2 = s2 ; falg = 1 ; ck = a[t2] ; while ( t1 < t2 ) { if ( a[t1] != a[t2] && ck != -1 ) { if ( a[t1] == ck ) t1++; else if (a[t2] == ck ) t2--; else { falg = 0 ; break ; } } if (a[t1] == a[t2] ) { t1++; t2--; } } break ; } if (a[t1] == a[t2] ) { t1++; t2--; } } if (falg) cout << "YESn" ; else cout << "NOn" ; } } return 0 ; }



