CF1324B Yet Another Palindrome Problem's Solution

· · 题解

CF1324B\ Yet\ Another\ Palindrome\ Problem's\ Solution

## $Problem ## $Solution $\qquad$那么再继续观察,发现一个很奇特的数据范围————"$a_i<n$",一般来说,这种数据范围只有一种可能,那就是要用$cnt$数组记录每个数出现了多少次(当然并不绝对,只是大多数情况)。 $\qquad$于是我们顺理成章想到一个很简单的做法,用$cnt$数组记录在$a[i]$之前出现过哪些数,分别出现了多少次,然后我们分情况讨论: $\qquad1^{\circ}cnt[a[i]]=0$,显然不可能出现以$a[i]$结尾的子序列。 $\qquad2^{\circ}cnt[a[i]]=1$,意味着在之前已经出现过一次$a[i]$。那么这样就可以肯定出现了回文子序列吗?当然不是,还有可能是$"AA"$。所以我们要多判断一次$a[i-1]$是不是等于$a[i]$。 $\qquad3^{\circ}cnt[a[i]]>1$,这种情况就不可能出现$"AA"$了,直接判断出现合法回文子序列。 ## Code ```cpp #include<bits/stdc++.h> using namespace std; const int N = 5e3 + 10; int T , n , a[N] , cnt[N] , flag; int main(){ scanf("%d" , &T); while(T--){ scanf("%d" , &n) , flag = false; for(int i = 1; i <= n; i++) scanf("%d" , &a[i]) , cnt[i] = 0; for(int i = 1; i <= n; i++){ if(cnt[a[i]] == 1 && a[i - 1] != a[i]) flag = true; else if(cnt[a[i]] > 1) flag = true; cnt[a[i]]++; } flag ? puts("YES") : puts("NO"); } return 0; } ``` 自认为码风还是比较清奇可看的,大家可以对着$Solution$仔细看一下。 最后,[$This\ is\ my\ blog$](https://www.luogu.com.cn/blog/A66-888/) , 有兴趣的可以逛一下,应该能有所收获。