// // main.cpp // Palindrom // // Created by Mikołaj Jędrzejewski on 12/8/22. // #include <iostream> #include <vector> using namespace std; // i is an index in the first half of the vector a inline bool right_place(vector<int> &a, int i) { return a[i] == a[a.size()-1-i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //cout<<"Hello my friend!\n"; vector<int> x; int as = 0, bs = 0; char c; while(cin.get(c)) if (c == '\n') break; else x.push_back(c == 'a'); for (int i=0; i<x.size(); ++i) if (x[i] == 1) as++; else bs++; if (as%2 == 1 and bs%2 == 1) { cout<<-1; return 0; } int ans = 0; int oks = 0; int a1 = 0; // a's in the first half but on wrong places int b1 = 0; // b's in the first half but on wrong places for (int i=0; i<x.size()/2; ++i) { if (right_place(x, i)) ++oks; else if (x[i] == 1) ++a1; else ++b1; } //if (as%2 == 0 or bs%2 == 0) //ans=a1+b1-min(a1, b1); ans = min(a1, b1) + (max(a1, b1) - min(a1, b1))/2; if ((as%2 == 1 and x[x.size()/2] == 0) or (bs%2 == 1 and x[x.size()/2] == 1)) ans++; cout<<ans;return 0; /*if (as%2 == 1) { if (x[ x.size()/2 +1 ] == 1) // bab ans=a1+b1-min(a1, b1); else { // bba, the b on the left is actually on the right place ans=a1+(b1-1)-min(a1, b1-1) + 1; } } if (bs%2 == 1) { if (x[ x.size()/2 +1 ] == 1) // bab ans=a1+b1-min(a1, b1); else { // aab, the b on the left is actually on the right place ans=(a1-1)+b1-min(a1-1, b1) + 1; } } cout<<ans; 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 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 | // // main.cpp // Palindrom // // Created by Mikołaj Jędrzejewski on 12/8/22. // #include <iostream> #include <vector> using namespace std; // i is an index in the first half of the vector a inline bool right_place(vector<int> &a, int i) { return a[i] == a[a.size()-1-i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //cout<<"Hello my friend!\n"; vector<int> x; int as = 0, bs = 0; char c; while(cin.get(c)) if (c == '\n') break; else x.push_back(c == 'a'); for (int i=0; i<x.size(); ++i) if (x[i] == 1) as++; else bs++; if (as%2 == 1 and bs%2 == 1) { cout<<-1; return 0; } int ans = 0; int oks = 0; int a1 = 0; // a's in the first half but on wrong places int b1 = 0; // b's in the first half but on wrong places for (int i=0; i<x.size()/2; ++i) { if (right_place(x, i)) ++oks; else if (x[i] == 1) ++a1; else ++b1; } //if (as%2 == 0 or bs%2 == 0) //ans=a1+b1-min(a1, b1); ans = min(a1, b1) + (max(a1, b1) - min(a1, b1))/2; if ((as%2 == 1 and x[x.size()/2] == 0) or (bs%2 == 1 and x[x.size()/2] == 1)) ans++; cout<<ans;return 0; /*if (as%2 == 1) { if (x[ x.size()/2 +1 ] == 1) // bab ans=a1+b1-min(a1, b1); else { // bba, the b on the left is actually on the right place ans=a1+(b1-1)-min(a1, b1-1) + 1; } } if (bs%2 == 1) { if (x[ x.size()/2 +1 ] == 1) // bab ans=a1+b1-min(a1, b1); else { // aab, the b on the left is actually on the right place ans=(a1-1)+b1-min(a1-1, b1) + 1; } } cout<<ans; return 0;*/ } |