QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#617790 | #7882. Linguistics Puzzle | hjxddl | RE | 0ms | 0kb | C++20 | 2.7kb | 2024-10-06 17:07:21 | 2024-10-06 17:07:22 |
answer
// Coded by hjxddl
#include <bits/stdc++.h>
#define ll long long
#define db double
#define v(x) std::vector<x>
const int N = 2e5 + 5;
int ans[60], n;
int ans1[60][60];
std::string s[60][60];
std::map<std::pair<int, int>, v(int)> mp;
std::map<char, int> id;
std::map<int, char> rid;
v(int) cnt1, cnt2, cnt3, cnt4;
bool dfs(int x) {
if (x == n + 1) {
v(int) cnt(n * n + 100);
int ans2[60];
for (int i = 0; i < n; i++)
ans2[ans[i]] = i;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cnt[i * j]++;
if (s[i][j].size() == 1)
cnt[ans2[id[s[i][j][0]]]]--;
else
cnt[ans2[id[s[i][j][0]]] * n + ans2[id[s[i][j][1]]]]--;
}
}
for (int i = 0; i <= n * n; i++)
if (cnt[i]) return 0;
return 1;
}
v(int) &num = mp[{cnt3[x], cnt4[x]}];
for (auto &y : num) {
// std::cerr << x << " " << y << " " << ans[y] << '\n';
if (ans[y]) continue;
ans[y] = x;
if (dfs(x + 1)) return 1;
ans[y] = 0;
}
return 0;
}
void init() {
for (int i = 0; i < n; i++)
ans[i] = 0;
mp.clear();
cnt1 = cnt2 = cnt3 = cnt4 = v(int)(n + 5);
}
void solve() {
std::cin >> n;
init();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> s[i][j];
cnt1[i * j % n]++;
if (i * j / n)
cnt2[i * j / n]++;
if (s[i][j].size() == 1) {
cnt3[id[s[i][j][0]]]++;
} else {
cnt3[id[s[i][j][1]]]++;
cnt4[id[s[i][j][0]]]++;
}
}
}
for (int i = 0; i < n; i++)
mp[{cnt1[i], cnt2[i]}].push_back(i);
// for (auto [_, x] : mp) {
// for (auto it : x) {
// std::cerr << it << " " << _.first << " " << _.second << '\n';
// }
// }
// for (int i = 1; i <= n; i++) {
// std::cerr << i << " " << cnt3[i] << " " << cnt4[i] << '\n';
// }
dfs(1);
for (int i = 0; i < n; i++) {
std::cout << rid[ans[i]];
}
std::cout << '\n';
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0), std::cout.tie(0);
for (int i = 0; i <= 25; i++) {
id['a' + i] = i + 1;
rid[i + 1] = char('a' + i);
}
for (int i = 26; i <= 51; i++) {
id['A' + i - 26] = i + 1;
rid[i + 1] = char('A' + i - 26);
}
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
std::cout << std::flush;
system("pause");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
2 3 a b a b b b b c cc 4 d d d d d c b a d b cd cb d a cb bc
output:
bca dcba