Advertisement
Dang_Quan_10_Tin

PALIND TS10 Da Nang 2021-2022

Jan 8th, 2022
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #define task "PALIND"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e4 + 5;
  12.  
  13. bool f[N][N];
  14. string X;
  15.  
  16. void Read()
  17. {
  18.     cin >> X;
  19. }
  20.  
  21. void Solve()
  22. {
  23.     // Trường hợp cơ sở
  24.     for (int i = 0; i < (int)X.size(); ++i)
  25.         f[i][i] = f[i + 1][i] = true;
  26.  
  27.     // Tính f[][]
  28.     for (int i = 2; i <= (int)X.size(); ++i)
  29.         for (int j = 0; j <= (int)X.size() - i; ++j)
  30.         {
  31.             f[j][j + i - 1] = f[j + 1][j + i - 2] && X[j] == X[j + i - 1];
  32.         }
  33.     string ans;
  34.  
  35.     for (int i = 0; i < (int)X.size(); ++i)
  36.         for (int j = (int)X.size() - 1; j >= i; --j)
  37.             if (f[i][j] && X[i] != '0') //  Không được có số 0 vô nghĩa
  38.             {
  39.                 if ((int)ans.size() < j - i + 1 || ((int)ans.size() == j - i + 1 && ans < X.substr(i, j - i + 1)))
  40.                     ans = X.substr(i, j - i + 1);
  41.                 break; // Dừng do ít chữ số hơn chắc chắn sẽ nhỏ hơn
  42.             }
  43.  
  44.     cout << ans.size() << "\n"
  45.          << ans;
  46. }
  47.  
  48. int32_t
  49. main()
  50. {
  51.     ios::sync_with_stdio(0);
  52.     cin.tie(0);
  53.     cout.tie(0);
  54.     if (fopen(task ".INP", "r"))
  55.     {
  56.         freopen(task ".INP", "r", stdin);
  57.         freopen(task ".OUT", "w", stdout);
  58.     }
  59.  
  60.     Read();
  61.     Solve();
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement