QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#392986 | #7882. Linguistics Puzzle | iwew | RE | 0ms | 3644kb | C++20 | 2.5kb | 2024-04-18 01:23:58 | 2024-04-18 01:23:59 |
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];
auto getid = [&](char ch) -> int {
if(ch <= 'z') return ch - 'a';
else return ch - 'A' + 26;
};
vector<int> c1(n, 0), c1_(n, 0), s1(n, 0), s1_(n, 0), s2(n, 0), s2_(n, 0);
vector<vector<int>> c(n, vector<int> (n, 0)), c_(n, vector<int> (n, 0));
for(int i = 0; i < n * n; i ++ ) {
if(s[i].length() == 1) {
c1[getid(s[i][0])] ++ ;
} else {
s1[getid(s[i][0])] ++ , s2[getid(s[i][1])] ++ ;
c[getid(s[i][0])][getid(s[i][1])] ++ ;
}
}
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
int v = i * j;
if(v < n) {
c1_[v] ++ ;
} else {
s1_[v / n] ++ , s2_[v % n] ++ ;
c_[v / n][v % n] ++ ;
}
}
}
vector<int> alpha;
vector<bool> has(n, false);
for(int i = 0; i < n; i ++ ) {
for(int j = i + 1; j < n; j ++ ) {
if(c1[i] == c1[j] && s1[i] == s1[j] && s2[i] == s2[j]) {
has[i] = has[j] = true;
}
}
}
for(int i = 0; i < n; i ++ ) {
if(has[i]) {
alpha.push_back(i);
}
}
vector<int> ans(n, -1);
vector<bool> used(n, false);
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
if(!has[i] && c1[i] == c1_[j] && s1[i] == s1_[j] && s2[i] == s2_[j]) {
ans[i] = j, used[j] = true;
}
}
}
vector<int> num;
for(int i = 0; i < n; i ++ ) {
if(!used[i]) {
num.push_back(i);
}
}
int len = (int)num.size();
vector<int> p(len);
for(int i = 0; i < len; i ++ ) p[i] = i;
do {
for(int i = 0; i < len; i ++ ) {
ans[num[p[i]]] = alpha[i];
}
bool ok = true;
for(int i = 0; i < n; i ++ ) {
if(c1[i] != c1_[ans[i]] || s1[i] != s1_[ans[i]] || s2[i] != s2_[ans[i]]) {
ok = false;
}
}
for(int i = 0; i < n; i ++ ) {
for(int j = 0; j < n; j ++ ) {
if(c[i][j] != c_[ans[i]][ans[j]]) {
ok = false;
}
}
}
if(ok) {
vector<int> res(n);
for(int i = 0; i < n; i ++ ) res[i] = i;
sort(res.begin(), res.end(), [&](int a, int b) {
return ans[a] < ans[b];
});
for(int i = 0; i < n; i ++ ) {
cout << (res[i] < 26 ? (char)(res[i] + 'a') : (char)(res[i] - 26 + 'A'));
}
cout << '\n';
break;
}
} while(next_permutation(p.begin(), p.end()));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
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: 3592kb
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 ...