#include <stdio.h> #include <stdlib.h> #include <iostream> #include <set> #define MAX_LEN 200000 using namespace std; int main(void) { long result = -1; static char data [MAX_LEN + 1]; set<int> pos [2]; char * beg = data, * end = beg; int offs = 0; if (scanf("%s", data)) /* antiwarning */ { } /* fill pos */ while (*end) { pos[*end++ == 'b'].insert(offs++); } #ifdef DBG {set<int>::iterator i; for(i = pos[0].begin(); i != pos[0].end(); ++i) { cerr << *i << " "; } cerr << "\n";} {set<int>::iterator i; for(i = pos[1].begin(); i != pos[1].end(); ++i) { cerr << *i << " "; } cerr << "\n";} dbgs(data); #endif if (!((pos[0].size() & 1) && (pos[1].size() & 1))) { result = 0; while (beg < --end) { set<int> & b = pos[*beg == 'b']; set<int> & e = pos[*end == 'b']; #ifdef DBG dbg(DBG_EOL); {set<int>::iterator i; for(i = pos[0].begin(); i != pos[0].end(); ++i) { cerr << *i << " "; } cerr << "\n";} {set<int>::iterator i; for(i = pos[1].begin(); i != pos[1].end(); ++i) { cerr << *i << " "; } cerr << "\n";} dbgv(beg - data); dbgs(data); #endif if (&b == &e) { b.erase(b.begin()); b.erase(--b.end()); ++beg; } else { int beg_len = abs(*b. begin() - *e. begin()); int end_len = abs(*b.rbegin() - *e.rbegin()); set<int>::iterator bi; set<int>::iterator ei; if (beg_len <= end_len) { bi = b.begin(); ei = e.begin(); result += beg_len; } else { bi = --b.end(); ei = --e.end(); result += end_len; } swap(data[*bi], data[*ei]); b.insert(*ei); e.insert(*bi); b.erase(bi); e.erase(ei); ++end; } } } printf("%ld\n", result); return EXIT_SUCCESS; }
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <stdio.h> #include <stdlib.h> #include <iostream> #include <set> #define MAX_LEN 200000 using namespace std; int main(void) { long result = -1; static char data [MAX_LEN + 1]; set<int> pos [2]; char * beg = data, * end = beg; int offs = 0; if (scanf("%s", data)) /* antiwarning */ { } /* fill pos */ while (*end) { pos[*end++ == 'b'].insert(offs++); } #ifdef DBG {set<int>::iterator i; for(i = pos[0].begin(); i != pos[0].end(); ++i) { cerr << *i << " "; } cerr << "\n";} {set<int>::iterator i; for(i = pos[1].begin(); i != pos[1].end(); ++i) { cerr << *i << " "; } cerr << "\n";} dbgs(data); #endif if (!((pos[0].size() & 1) && (pos[1].size() & 1))) { result = 0; while (beg < --end) { set<int> & b = pos[*beg == 'b']; set<int> & e = pos[*end == 'b']; #ifdef DBG dbg(DBG_EOL); {set<int>::iterator i; for(i = pos[0].begin(); i != pos[0].end(); ++i) { cerr << *i << " "; } cerr << "\n";} {set<int>::iterator i; for(i = pos[1].begin(); i != pos[1].end(); ++i) { cerr << *i << " "; } cerr << "\n";} dbgv(beg - data); dbgs(data); #endif if (&b == &e) { b.erase(b.begin()); b.erase(--b.end()); ++beg; } else { int beg_len = abs(*b. begin() - *e. begin()); int end_len = abs(*b.rbegin() - *e.rbegin()); set<int>::iterator bi; set<int>::iterator ei; if (beg_len <= end_len) { bi = b.begin(); ei = e.begin(); result += beg_len; } else { bi = --b.end(); ei = --e.end(); result += end_len; } swap(data[*bi], data[*ei]); b.insert(*ei); e.insert(*bi); b.erase(bi); e.erase(ei); ++end; } } } printf("%ld\n", result); return EXIT_SUCCESS; } |