QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#392954 | #7882. Linguistics Puzzle | iwew | RE | 0ms | 3688kb | C++20 | 3.0kb | 2024-04-18 00:20:23 | 2024-04-18 00:20:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T -- ) {
int n;
cin >> n;
vector<string> s(n * n);
for(int i = 0; i < n * n; i ++ ) cin >> s[i];
int cnt = 0;
vector<array<int, 3>> seq(n);
map<array<int, 3>, int> mp;
for(int i = 0; i < n; i ++ ) {
char ch = (i >= 26 ? (char)(i - 26 + 'A') : (char)(i + 'a'));
array<int, 3> tmp = {0, 0, 0};
for(auto str : s) {
if(str.length() == 1 && str[0] == ch) {
tmp[0] ++ ;
} else {
if(str[0] == ch) tmp[1] ++ ;
if(str[1] == ch) tmp[2] ++ ;
}
}
seq[i] = tmp;
if(!mp.count(tmp)) mp[tmp] = cnt ++ ;
}
vector<array<int, 3>> arr(n);
vector<vector<int>> digit(cnt);
for(int v = 0; v < n; v ++ ) {
array<int, 3> tmp = {0, 0, 0};
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
int val = i * j;
if(val < n && val == v) {
tmp[0] ++ ;
}
if(val >= n) {
int v1 = val / n, v2 = val % n;
if(v1 == v) tmp[1] ++ ;
if(v2 == v) tmp[2] ++ ;
}
}
}
arr[v] = tmp;
digit[mp[tmp]].push_back(v);
}
string ans = string(n, '$');
vector<vector<char>> alpha(cnt);
for(int i = 0; i < n; i ++ ) {
if((int)digit[mp[seq[i]]].size() == 1) {
int pos = digit[mp[seq[i]]][0];
ans[pos] = (i >= 26 ? (char)(i - 26 + 'A') : (char)(i + 'a'));
} else {
char ch = (i >= 26 ? (char)(i - 26 + 'A') : (char)(i + 'a'));
alpha[mp[seq[i]]].push_back(ch);
}
}
auto get = [&](char ch) -> int {
if(ch == '$') return -1;
else if(ch <= 'z') return ch - 'a';
else return ch - 'A' + 26;
};
auto check = [&](string str) -> bool {
for(int i = 0; i < n; i ++ ) {
if(str[i] == '$') {
return false;
}
}
vector<int> p(n);
for(int i = 0; i < n; i ++ ) p[i] = i;
sort(p.begin(), p.end(), [&](int a, int b) {
return str[a] < str[b];
});
vector<int> nums(2 * n * n, 0);
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
nums[i * j] ++ ;
}
}
for(auto tmps : s) {
int val = 0;
for(auto c : tmps) {
if(get(c) == -1) return false;
val = val * n + p[get(c)];
}
nums[val] -- ;
}
for(int i = 0; i < n * n; i ++ ) {
if(nums[i] != 0) {
return false;
}
}
return true;
};
auto dfs = [&](auto &&dfs, int u) -> bool {
if(u == cnt) {
if(check(ans)) {
cout << ans << '\n';
return true;
}
return false;
}
if(alpha[u].empty()) {
return dfs(dfs, u + 1);
}
int len = (int)alpha[u].size();
vector<int> p(len);
for(int i = 0; i < len; i ++ ) p[i] = i;
do {
for(int i = 0; i < len; i ++ ) {
ans[digit[u][i]] = alpha[u][p[i]];
}
if(dfs(dfs, u + 1)) return true;
} while(next_permutation(p.begin(), p.end()));
return false;
};
dfs(dfs, 0);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3688kb
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
result:
ok OK
Test #2:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
2 4 d a a bc ba bc b a a a d a a cb c c 4 a b da b b d ad b db b a c da b c b
output:
abcd bdac
result:
ok OK
Test #3:
score: -100
Runtime Error
input:
50 3 b b b a a c b b cc 4 d ab c ad d b ba ab c b d d d d d a 5 a aa aa ab ab ae b b e c c c ba c c c c dd d d dd c e c e 6 a ca a a a a a a ce a a b ba ba bc bc bd be e c c ca a cd cd be d d dc dc e e a eb f f 7 a a a a a a a a cf a a a a b b b b c c c cf a dd d dc d dd e f ed ee ee fb eg eg eg eg ...