QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#106592 | #3168. Letter Wheels | skittles1412# | TL | 2ms | 3448kb | C++17 | 2.2kb | 2023-05-18 09:20:54 | 2023-05-18 09:20:55 |
Judging History
answer
#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using u64 = uint64_t;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
struct chash {
u64 operator()(u64 x) const {
x ^= x << 17;
x ^= x >> 33;
x ^= x << 11;
x ^= x >> 23;
return x;
}
};
template <int MOD>
int shash(const string& s, int base) {
long ans = 0;
for (auto& a : s) {
ans = (ans * base + a - 'A' + 1) % MOD;
}
return int(ans);
}
u64 shash(const string& s) {
constexpr int m1 = 1e9 + 7, m2 = 1e9 + 9;
return (u64(shash<m1>(s, 5)) << 32) | shash<m2>(s, 5);
}
int solve(string a, string b, string c) {
int n = sz(a);
__gnu_pbds::gp_hash_table<u64, int, chash> mp;
for (int i = 0; i < n; i++) {
auto it = mp.insert({shash(c), i}).first;
it->second = min(it->second, i);
rotate(c.begin(), c.begin() + 1, c.end());
}
int ans = 1e9;
for (int i = 0; i < n; i++) {
string nc;
for (int j = 0; j < n; j++) {
nc += 'A' ^ 'B' ^ 'C' ^ a[j] ^ b[j];
}
auto it = mp.find(shash(nc));
if (it != mp.end()) {
ans = min(ans, i + it->second);
}
rotate(b.begin(), b.begin() + n - 1, b.end());
}
return ans;
}
void solve() {
string arr[3];
for (auto& a : arr) {
cin >> a;
}
int ans = 1e9, perm[3] {0, 1, 2};
do {
ans = min(ans, solve(arr[perm[0]], arr[perm[1]], arr[perm[2]]));
} while (next_permutation(begin(perm), end(perm)));
if (ans == 1e9) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3436kb
input:
ABC ABC ABC
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3396kb
input:
ABBBAAAA BBBCCCBB CCCCAAAC
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3448kb
input:
AABB BBCC ACAC
output:
-1
result:
ok single line: '-1'
Test #4:
score: -100
Time Limit Exceeded
input:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...