#include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(0); #define ll long long using namespace std; int ruch(string s) { ll n = s.length(); ll count = 0; for (int i = 0; i <n / 2; i++) { ll left = i, right = n - left - 1; while (left <right) { if (s[left] == s[right]) { break; } else { right--; } }if (left == right) { return -1; } for (int j = right; j <n - left - 1; j++) { swap(s[j], s[j + 1]); count++; } } return count; } int main() { string s; cin>>s; if(s.size()==1){ cout<<-1; return 0; } int ans1 = ruch(s); reverse(s.begin(), s.end()); int ans2 = ruch(s); cout <<max(ans1, ans2); 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 42 43 44 45 46 47 | #include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(0); #define ll long long using namespace std; int ruch(string s) { ll n = s.length(); ll count = 0; for (int i = 0; i <n / 2; i++) { ll left = i, right = n - left - 1; while (left <right) { if (s[left] == s[right]) { break; } else { right--; } }if (left == right) { return -1; } for (int j = right; j <n - left - 1; j++) { swap(s[j], s[j + 1]); count++; } } return count; } int main() { string s; cin>>s; if(s.size()==1){ cout<<-1; return 0; } int ans1 = ruch(s); reverse(s.begin(), s.end()); int ans2 = ruch(s); cout <<max(ans1, ans2); return 0; } |