Advertisement
skimono

Untitled

Mar 5th, 2024
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #pragma optimize ("Ofast");
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <set>
  7. #include <algorithm>
  8. #include <map>
  9. #include <string>
  10. #include <unordered_map>
  11. #include <cassert>
  12. #include <bitset>
  13.  
  14. using namespace std;
  15. //#define int long long
  16. const int inf = 2e9;
  17. const int mod = 998244353;
  18.  
  19. unsigned long long x[15001], y[15001], pw[15001];
  20. unsigned long long xx[15001], yy[15001];
  21. const unsigned long long p = 347;
  22. int keys[256];
  23. int zxc[64][6];
  24. int n, i, j, g;
  25.  
  26. void solve() {
  27.     vector <char> can;
  28.     can.push_back('#');
  29.     can.push_back('$');
  30.     pw[0] = 1;
  31.     for (i = 1; i <= 15000; i++) {
  32.         pw[i] = pw[i - 1] * p;
  33.     }
  34.     for (i = 0; i < 10; i++) {
  35.         can.push_back('0' + i);
  36.     }
  37.     for (i = 0; i < 26; i++) {
  38.         can.push_back(i + 'A');
  39.         can.push_back(i + 'a');
  40.     }
  41.     sort(can.begin(), can.end());
  42.     for (i = 0; i < 64; i++) {
  43.         keys[can[i]] = i;
  44.     }
  45.     for (i = 0; i < 64; i++) {
  46.         for (j = 0; j < 6; j++) {
  47.             if (i & (1 << j)) {
  48.                 zxc[i][5 - j] = 1;
  49.             }
  50.             else {
  51.                 zxc[i][5 - j] = 0;
  52.             }
  53.         }
  54.     }
  55.     int k, m;
  56.     cin >> k >> m;
  57.     n = k * 6;
  58.     int last, idx;
  59.     string s1, s2;
  60.     int cx, cy;
  61.     while (m--) {
  62.         cin >> s1 >> s2;
  63.         last = n - 1;
  64.         for (i = 0; i < s1.size(); i++) {
  65.             idx = keys[s1[i]];
  66.             for (j = 0; j < 6; j++) {
  67.                 xx[last] = zxc[idx][j];
  68.                 last--;
  69.             }
  70.         }
  71.         last = n - 1;
  72.         for (i = 0; i < s2.size(); i++) {
  73.             idx = keys[s2[i]];
  74.             for (j = 0; j < 6; j++) {
  75.                 yy[last] = zxc[idx][j];
  76.                 last--;
  77.             }
  78.         }
  79.         for (i = 0; i < n; i++) {
  80.             x[i + 1] = x[i] * p + xx[i];
  81.             y[i + 1] = y[i] * p + yy[i];
  82.         }
  83.         if (x[n] == y[n]) {
  84.             cout << 0 << '\n';
  85.             continue;
  86.         }
  87.         for (i = 1; i < n; i++) {
  88.             if (x[n] == (y[n] - y[i] * pw[n - i]) * pw[i] + y[i]) {
  89.                 cout << i << '\n';
  90.                 break;
  91.             }
  92.         }
  93.         if (i == n) {
  94.             cout << -1 << '\n';
  95.         }
  96.     }
  97. }
  98.  
  99. signed main() {
  100. #ifdef _DEBUG
  101.     freopen("input.txt", "r ", stdin);
  102.     freopen("output.txt", "w", stdout);
  103. #endif
  104.     ios_base::sync_with_stdio(0);
  105.     cin.tie(NULL);
  106.     cout.tie(NULL);
  107.     int t = 1;
  108.     //cin >> t;
  109.     while (t--) solve();
  110. }
  111. //Deisgned by skimono
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement