#include <iostream> using namespace std; /** * @Author: Robert Rośczak LO nr. 1 w Łodzi. * @Problem: Palinrom (pal) */ int main() { int res = 0; bool isNull = false; string a; cin >> a; for (int i = 0; i < a.length()/2; ++i) { if (a[i] != a[a.length()-i-1]) { int b = i, c = i; while (a[b] != a[a.length()-i-1]) { b++; if (b == a.length()-i-1) { if (isNull) { cout << -1; return 0; } isNull = true; b = a.length()-i-1; c = a.length()/2; i--; break; } } for (int d = b; d > c;d--) { char g = a[d]; a[d] = a[d-1]; a[d-1] = g; res++; } } } cout << res; return 0; }
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 | #include <iostream> using namespace std; /** * @Author: Robert Rośczak LO nr. 1 w Łodzi. * @Problem: Palinrom (pal) */ int main() { int res = 0; bool isNull = false; string a; cin >> a; for (int i = 0; i < a.length()/2; ++i) { if (a[i] != a[a.length()-i-1]) { int b = i, c = i; while (a[b] != a[a.length()-i-1]) { b++; if (b == a.length()-i-1) { if (isNull) { cout << -1; return 0; } isNull = true; b = a.length()-i-1; c = a.length()/2; i--; break; } } for (int d = b; d > c;d--) { char g = a[d]; a[d] = a[d-1]; a[d-1] = g; res++; } } } cout << res; return 0; } |