Advertisement
IanisBelu

INVESORT - Inversion Sort

Nov 7th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | Source Code | 0 0
  1. #ifdef LOCAL
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <vector>
  7. #include <queue>
  8. #include <unordered_map>
  9. #include <cstring>
  10. #include <map>
  11. #else
  12. #pragma GCC optimize("Ofast,unroll-loops")
  13. #include <bits/stdc++.h>
  14. #endif
  15.  
  16. #ifndef LOCAL
  17. #define endl '\n'
  18. #endif
  19.  
  20. #define sz(a) ((int)(a).size())
  21. #define all(a) (a).begin(), (a).end()
  22.  
  23. #define fi first
  24. #define se second
  25.  
  26. #define YES cout << "YES" << endl
  27. #define NO cout << "NO" << endl
  28.  
  29. #define lsb(x) (x & (-x))
  30. #define popcount(x) __builtin_popcount(x)
  31.  
  32. using namespace std;
  33.  
  34. #define double long double
  35.  
  36. typedef long long ll;
  37. typedef pair<int, int> pii;
  38.  
  39. const int INF = 1e9+5;
  40.  
  41. int n = 10;
  42. char a[15], b[15];
  43. unordered_map<string, int> d;
  44.  
  45. void read() {
  46.     cin >> a >> b;
  47.     if (a[0] == '*') exit(0);
  48. }
  49.  
  50. // Am precalculat distantele de la "abcdefghej"
  51. void bfs(string s) {
  52.     queue<string> q;
  53.     d[s] = 1;
  54.     q.push(s);
  55.     while (!q.empty()) {
  56.         s = q.front();
  57.         int dist = d[s];
  58.         q.pop();
  59.         for (int i = 0; i < n; i++) {
  60.             for (int j = i + 1; j < n; j++) {
  61.                 reverse(s.begin() + i, s.begin() + j + 1);
  62.                 if (d[s] == 0) {
  63.                     d[s] = dist + 1;
  64.                     q.push(s);
  65.                 }
  66.                 reverse(s.begin() + i, s.begin() + j + 1);
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. int solve() {
  73.     string s(n, 'a');
  74.     char c = 'a';
  75.     // Am transformat sirul b a.i. dist(a, b) = dist("abcdefghij", b')
  76.     for (int i = 0; i < n; i++) {
  77.         for (int j = 0; j < n; j++)
  78.             if (a[i] == b[j])
  79.                 s[j] = c++;
  80.     }
  81.     return d[s] - 1; // Distanta de la b' la "abcdefghij"
  82. }
  83.  
  84. signed main() {
  85. #ifdef LOCAL
  86.     freopen("input.txt", "r", stdin);
  87. #else
  88.     ios_base::sync_with_stdio(false);
  89.     cin.tie(0);
  90.     cout.tie(0);
  91. #endif
  92.     bfs("abcdefghij");
  93.     while (true)  {
  94.         read();
  95.         cout << solve() << endl;
  96.     }
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement