#include <bits/stdc++.h> using namespace std; int parse(string &s, int a, int b) { if (a >= b) return 0; if (s[a] == s[b]) return parse(s, a + 1, b - 1); if (a + 1 == b) return -1; if (s[a] != s[a + 1]) swap(s[a], s[a + 1]); else swap(s[b], s[b - 1]); int ans = parse(s, a + 1, b - 1); if (ans == -1) return -1; return ans + 1; } int main() { string s; cin >> s; cout << parse(s, 0, s.size() - 1) << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <bits/stdc++.h> using namespace std; int parse(string &s, int a, int b) { if (a >= b) return 0; if (s[a] == s[b]) return parse(s, a + 1, b - 1); if (a + 1 == b) return -1; if (s[a] != s[a + 1]) swap(s[a], s[a + 1]); else swap(s[b], s[b - 1]); int ans = parse(s, a + 1, b - 1); if (ans == -1) return -1; return ans + 1; } int main() { string s; cin >> s; cout << parse(s, 0, s.size() - 1) << endl; } |