Advertisement
wym1111

Untitled

Nov 25th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e3 + 10;
  5.  
  6. int n, a[N];
  7.  
  8. bool check(int l, int r) {
  9.     while (l < r) {
  10.         if (a[l] != a[r]) return 0;
  11.         l ++;
  12.         r --;
  13.     }
  14.     return 1;
  15. }
  16.  
  17. int main () {
  18.     cin >> n;
  19.     for (int i = 1; i <= n; i ++) {
  20.         cin >> a[i];
  21.     }
  22.     int len = 0; //记录已经出现过的最长回文数组长度
  23.     int l, r;
  24.     for (int i = 1; i <= n; i ++) {
  25.         for (int j = i; j <= n; j ++) {
  26.             // 判断 (i,j)是否是回文数组
  27.             if (j - i + 1 <= len) continue;
  28.             if (check(i, j)) {
  29.                 len = j - i + 1;
  30.                 l = i;
  31.                 r = j;
  32.             }
  33.         }
  34.     }
  35.     for (int i = l; i <= r; i ++) {
  36.         cout << a[i] << ' ';
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement