Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <map>
- #include <vector>
- //#include<bits/stdc++.h>
- using namespace std;
- const int INF = 1e8;
- void swap_characters(int S, int E, string & str) {
- if(S < E) {
- char c = str[S];
- for(int i = S; i < E; i++) {
- str[i] = str[i + 1];
- }
- str[E] = c;
- }
- else {
- char c = str[S];
- for(int i = S; i > E; i--) {
- str[i] = str[i - 1];
- }
- str[E] = c;
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- string s;
- cin >> s;
- vector<int> cnt(26, 0);
- for(int i = 0; i < n; i++) {
- cnt[s[i] - 'a']++;
- }
- int odd = 0;
- for(int i = 0; i < 26; i++) {
- if(cnt[i] % 2 == 1) {
- odd++;
- }
- }
- if(odd > 1) {
- cout << "GRESHKA" << endl;
- return 0;
- }
- int res = 0;
- for(int i = 0; i < n / 2; i++) {
- if(s[i] != s[n - i - 1]) {
- int E = INF;
- int S = -INF;
- for(int j = i + 1; j < n - i - 1; j++) {
- if(s[j] == s[n - i - 1]) {
- E = j;
- break;
- }
- }
- for(int j = n - i - 2; j > i; j--) {
- if(s[j] == s[i]) {
- S = j;
- break;
- }
- }
- if(E - i < n - i - 1 - S) {
- swap_characters(E, i, s);
- res += E - i;
- }
- else {
- swap_characters(S, n - i - 1, s);
- res += n - i - 1 - S;
- }
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement