QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275926#7882. Linguistics PuzzleSGColin#TL 1ms3776kbC++201.8kb2023-12-05 12:05:572023-12-05 12:05:59

Judging History

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

  • [2023-12-05 12:05:59]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3776kb
  • [2023-12-05 12:05:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

#define N 60

int n;

vector<char> sc[N];

bool use[256];

char ans[N];

multiset<string> res;

bool check() {
    multiset<string> tmp;
    rep(i, 0, n - 1) rep(j, 0, n - 1) {
        string nw = "";
        int w = i * j;
        if (w < n) nw += ans[w];
        else {nw += ans[w / n]; nw += ans[w % n];}
        tmp.insert(nw);
    }
    return tmp == res;
}

bool dfs(int pos) {
    if (pos == n) return check();
    for (auto x : sc[pos]) 
        if (!use[x]) {
            use[x] = true;
            ans[pos] = x;
            if (dfs(pos + 1)) return true;
            use[x] = false;
        }
    return false;
}

inline void work() {
    cin >> n;
    res.clear();
    memset(use, 0, sizeof(use));
    vector<int> cnt1(n, 0), cnt2(n, 0);
    rep(i, 0, n - 1) rep(j, 0, n - 1) {
        int w = i * j;
        ++cnt2[w % n];
        w = w / n;
        if (w) ++cnt1[w];
    }
    map<pii, vector<int>> f;
    rep(i, 0, n - 1) f[{cnt1[i], cnt2[i]}].push_back(i);

    map<char, int> c1, c2;
    set<char> ch;
    rep(i, 1, n * n) {
        string s; cin >> s;
        res.insert(s);
        int l = s.length();
        if (l == 1) {++c2[s[0]]; ch.insert(s[0]);}
        else {
            ++c1[s[0]]; ch.insert(s[0]);
            ++c2[s[1]]; ch.insert(s[1]);
        }
    }
    for (auto x : ch) {
        pii c = make_pair(c1[x], c2[x]);
        for (auto p : f[c]) sc[p].push_back(x);
    }
    dfs(0);
    rep(i, 0, n - 1) putchar(ans[i]); puts("");
}

int main() {
    int t; cin >> t; while (t--) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
Time Limit Exceeded

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

result: