Advertisement
Josif_tepe

Untitled

Dec 14th, 2023
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <queue>
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n;
  13.     cin >> n;
  14.     string s;
  15.     cin >> s;
  16.     if(n > 30) {
  17.         cout << "GRESHKA" << endl;
  18.         return 0;
  19.     }
  20.     queue<string> q_s;
  21.     queue<int> q;
  22.     q_s.push(s);
  23.     q.push(0);
  24.     unordered_map<string, bool> visited;
  25.     visited[s] = true;
  26.     while(!q_s.empty()) {
  27.         string t = q_s.front();
  28.         q_s.pop();
  29.         int cekor = q.front();
  30.         q.pop();
  31.         bool ok = true;
  32.         int i = 0, j = n - 1;
  33.         while(i < j) {
  34.             if(t[i] != t[j]) {
  35.                 ok = false;
  36.                 break;
  37.             }
  38.             i++;
  39.             j--;
  40.         }
  41.         if(ok) {
  42.             cout << cekor << endl;
  43.             return 0;
  44.         }
  45.         for(int i = 0; i + 1 < n; i++) {
  46.             swap(t[i], t[i + 1]);
  47.             if(!visited[t]) {
  48.                 visited[t] = true;
  49.                 q_s.push(t);
  50.                 q.push(cekor + 1);
  51.             }
  52.             swap(t[i], t[i + 1]);
  53.            
  54.         }
  55.     }
  56.     cout << "GRESHKA" << endl;
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement