-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextHigherPalindromic.cpp
88 lines (75 loc) · 2.18 KB
/
nextHigherPalindromic.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v) {for (auto &it : v)cin >> it; return istream;}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
#define all(x) x.begin(), x.end()
// 1 2 3 4 5
// 1 2 3 2 1
// 1 2 9 1 1
// 1 3 0 3 1
// 9 9 9 2 2
// 1 0 0 0 2 2
// 1 0 0 0 0 1 (insert(s.begin(), 1))
// 1 9 9 1 1
void makePalindrom(string &s, int n) {
for (int i = n / 2; i > 0; i--) {
s[n - i] = s[i - 1];
}
}
void solve() {
string s;
cin >> s;
if (s.length() == 1) {cout << s << endl; return;}
string temp = s;
int n = s.length();
makePalindrom(temp, n);
if (stoi(s) < stoi(temp)) {
cout << temp << endl;
} else {
int mid = (n + 1) / 2;
if (s[mid] != '9') {
s[mid - 1] = (char)(s[mid - 1] + 1);
makePalindrom(s, n);
cout << s << endl;
} else {
int i;
for (i = mid; i > 0; i--) {
if (s[i - 1] != '9') {
s[i - 1] = (char)(s[i - 1] + 1);
break;
} else {
s[i - 1] = '0';
}
}
if (s[0] == '0') {
s.insert(s.begin(), '1');
makePalindrom(s, n + 1);
} else {
makePalindrom(s, n);
}
cout << s << endl;
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}