QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277464#7882. Linguistics PuzzlesupepapupuWA 3ms3712kbC++173.1kb2023-12-06 19:11:252023-12-06 19:11:26

Judging History

你现在查看的是最新测评结果

  • [2023-12-06 19:11:26]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3712kb
  • [2023-12-06 19:11:25]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;

int get(int x) {
    if (x >= 'a') return x - 'a';
    return 26 + x - 'A';
}

int n, idx;
array<int, 3> cnt1[52];
array<int, 3> cnt2[52];
map<array<int, 3>, int> mp;

int get(array<int, 3> a) {
    if (!mp.count(a)) mp[a] = idx++;
    return mp[a];
}

void solve() {
    cin >> n;
    mp.clear();
    idx = 0;
    for (int i = 0; i < n; ++i) cnt1[i] = cnt2[i] = {};
    vector<string> s(n * n);
    for (int i = 0; i < n * n; ++i) {
        int x = i / n, y = i % n;
        int t = x * y;
        int xx = t / n, yy = t % n;
        if (xx == yy) {
            if (xx == 0) ++cnt1[xx][1];
            else ++cnt1[xx][2];
        } else {
            if (xx != 0) ++cnt1[xx][0];
            ++cnt1[yy][1];
        }
        cin >> s[i];
        if (s[i].size() == 1) x = 0, y = get(s[i][0]);
        else x = get(s[i][0]), y = get(s[i][1]);
        if (x == y) {
            if (s[i].size() == 1) ++cnt2[x][1];
            else ++cnt2[x][2];
        } else {
            if (s[i].size() == 2) ++cnt2[x][0];
            ++cnt2[y][1];
        }
    }
    for (int i = 0; i < n; ++i) get(cnt1[i]);
    vector<vector<int>> nums1(idx), nums2(idx);
    for (int i = 0; i < n; ++i) {
        nums1[get(cnt1[i])].emplace_back(i);
        assert(mp.count(cnt2[i]));
        nums2[get(cnt2[i])].emplace_back(i);
    }
    for (int i = 0; i < idx; ++i) sort(nums1[i].begin(), nums1[i].end());
    vector<int> p(n);
    auto check = [&]()->bool {
        vector<int> cnt(n * n);
        for (int i = 0; i < n * n; ++i) {   
            int x = i / n, y = i % n;
            ++cnt[x * y];
            if (s[i].size() == 1) x = 0, y = p[get(s[i][0])];
            else x = p[get(s[i][0])], y = p[get(s[i][1])];
            --cnt[x * n + y];
        }
        return *max_element(cnt.begin(), cnt.end()) == 0;
    };
    int ret = 0;
    function<void(int)> dfs = [&](int k) {
        if (ret) return;
        if (k == idx) {
            if (check()) {
                for (int i = 0; i < n; ++i) 
                    for (int j = 0; j < n; ++j) 
                        if (p[j] == i) {
                            if (j < 26) cout << (char)('a' + j);
                            else cout << (char)('A' + j - 26);
                        }
                cout << el;
                ret = 1;
            }
            return;
        }
        do {
            for (int i = 0; i < nums1[k].size(); ++i) {
                p[nums2[k][i]] = nums1[k][i];
            }
            dfs(k + 1);
            if (ret) return;
        } while (next_permutation(nums1[k].begin(), nums1[k].end()));
        reverse(nums1[k].begin(), nums1[k].end());
    };
    dfs(0);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int tcase = 1;
    cin >> tcase;
    while (tcase--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3496kb

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: 3524kb

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: 0
Accepted
time: 3ms
memory: 3712kb

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 ...

output:

bca
dabc
cadbe
abcdef
aefdcgb
fcheabgd
bhgfedcia
jhcgfideba
fjbadkegcih
klhgjbaedcif
igkjmclfedhba
nflijahgmbdcek
anmlfijbgkhdceo
nofmlkjchdbegipa
aponblgjihcfqdkme
iqmonhckfrpgjedlba
prisodmbkjqghfencla
tcrdpoaklmjihfgeqsbn
utiraponmlksghjfecdbq
qotsrvjunmlkpiegfhdcba
pvutsrhwoimlkjnqgfedbca
xbvuts...

result:

ok OK

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 3608kb

input:

50
3
a a b c c c c bb c
4
a c ab b c ba ba c c a bc c c c d d
5
a a a b b b b b b c b d c b cc cc b d de e ea ed ed ee ee
6
a a ac ac b b b be ef c ca cb ea f cf d d e ca eb eb ec f ef c f f f f f f f f f cf ec
7
ag a a a ag b bb bb bd bd bf bf bf e bg c c c c c c c c c c c e c ga d da dd dd bf e e ...

output:

cba
cbad
becda
fecabd
cbgdafe
abgfechd
abhgeicdf
ahfdgcebij
bagedfcihjk
abcdhkgifjle
fkgdeachblijm
alcdbfgjihkmen
jfobedghiaklmcn
ahgdemcnijklfbop
aecdjlbhiqkfmnopg
aecdbfjhkimlgnopqr
afcdqbghriklmnopejs
dbcaefghijktmnosqrpl
qbisefgpcjklmnohdratu
pdcbeaghijmlknofqrstuv
aoedqfghijkclnbpmrstuvw
atcdef...

result:

wrong answer invalid length at case #47