Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <map>
- #include <set>
- #include <queue>
- #include <bits/stdc++.h>
- 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<string> q_s;
- queue<int> q;
- q_s.push(s);
- q.push(0);
- unordered_map<string, bool> 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement