CCCPaste

Untitled

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    string s;
    cin >> s;
    if(n > 30) {
        cout << "GRESHKA" << endl;
        return 0;
    }
    queue q_s;
    queue q;
    q_s.push(s);
    q.push(0);
    unordered_map visited;
    visited[s] = true;
    while(!q_s.empty()) {
        string t = q_s.front();
        q_s.pop();
        int cekor = q.front();
        q.pop();
        bool ok = true;
        int i = 0, j = n - 1;
        while(i < j) {
            if(t[i] != t[j]) {
                ok = false;
                break;
            }
            i++;
            j--;
        }
        if(ok) {
            cout << cekor << endl;
            return 0;
        }
        for(int i = 0; i + 1 < n; i++) {
            swap(t[i], t[i + 1]);
            if(!visited[t]) {
                visited[t] = true;
                q_s.push(t);
                q.push(cekor + 1);
            }
            swap(t[i], t[i + 1]);
            
        }
    }
    cout << "GRESHKA" << endl;
    return 0;
}