QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#648941#7882. Linguistics PuzzleAlphaZeWA 3ms3932kbC++203.6kb2024-10-17 21:02:372024-10-17 21:02:37

Judging History

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

  • [2024-10-17 21:02:37]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3932kb
  • [2024-10-17 21:02:37]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define x first
#define y second

#define ll long long

const int N = 55;
int ff[505]; char f[N];

int n; bool fl;

pii str[N * N], num[N * N];

int nm1[N * N], nm2[N * N];

int ton[N][4], ton2[N][4]; 

int a[N];

ll pown2[4], has[N], has2[N];
map<ll, vector<int>> mp;
int vis[N], fvis[N]; // 字母->数字

void calc(int x, int id) {
    int fi = x / n, se = x % n;
    num[id] = make_pair(fi, se);
    if (!fi) ++ton[se][0];
    else {
        if (fi == se) ++ton[fi][3];
        else ++ton[fi][1], ++ton[se][2];
    }
}

void calcstr(int id) {
    int fi = str[id].x, se = str[id].y;
    if (fi == -1) ++ton2[se][0];
    else if (fi == se) ++ton2[fi][3];
    else ++ton2[fi][1], ++ton2[se][2];
}

bool cmp(int x, int y) {
    return mp[has[x]].size() < mp[has[y]].size();
}

bool cmp2(pii a, pii b) {
    return vis[a.x] * n + vis[a.y] < vis[b.x] * n + vis[b.y];
}

bool check() {
    for (int i = 1; i <= n * n; ++i)
        nm2[i] = ((str[i].x == -1) ? 0 : (vis[str[i].x] * n)) + vis[str[i].y];
    sort(nm2 + 1, nm2 + n * n + 1);
    // for (int i = 1; i <= n * n; ++i) printf("%d ", nm2[i]); putchar('\n');
    for (int i = 1; i <= n * n; ++i)
        if (nm1[i] != nm2[i]) {
            // printf("nm1[%d] = %d, nm2[%d] = %d; \n", i, nm1[i], i, nm2[i]);
            return false;
        }
    return true;
}

void print() {
    for (int i = 0; i < n; ++i) fvis[vis[i]] = i;
    for (int i = 0; i < n; ++i) printf("%c", f[fvis[i]]); putchar('\n');
}

void dfs(int step) {
    if (fl) return;
    if (step == n) {
        // puts("sb");
        if (check()) {
            print();
            fl = 1;
        }      
        return;
    }
    for (auto x : mp[has[a[step]]]) {
        if (vis[x]) continue;
        vis[x] = step;
        dfs(step + 1);
        vis[x] = 0;
    }
}

void work() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < 4; ++j) ton[i][j] = ton2[i][j] = 0;
    for (int i = 1; i <= n * n; ++i) {
        char s[4];
        scanf("%s", s + 1);
        int len = strlen(s + 1);
        if (len == 1) str[i].x = -1, str[i].y = ff[s[1]];
        else str[i].x = ff[s[1]], str[i].y = ff[s[2]];
        calcstr(i);
    }
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            calc(i * j, i * n + j + 1), nm1[i * n + j + 1] = i * j;
    sort(nm1 + 1, nm1 + n * n + 1);
    // for (int i = 1; i <= n * n; ++i) printf("%d ", nm1[i]); putchar('\n');
    pown2[0] = 1; for (int i = 1; i < 4; ++i) pown2[i] = pown2[i - 1] * (n * n + 1);
    for (int i = 0; i < n; ++i) {
        has[i] = 0;
        for (int j = 0; j < 4; ++j) has[i] += pown2[j] * ton[i][j];
        // printf("i = %d, {%d, %d}, hash = %lld; {%d, %d, %d, %d} \n", i, num[i].x, num[i].y, has[i], ton[i][0], ton[i][1], ton[i][2], ton[i][3]);
    }  
    for (int i = 0; i < n; ++i) {
        has2[i] = vis[i] = 0;
        for (int j = 0; j < 4; ++j) has2[i] += pown2[j] * ton2[i][j];
        // printf("i = %d, {%d, %d}, hash = %lld; {%d, %d, %d, %d} \n", i, str[i].x, str[i].y, has2[i], ton2[i][0], ton2[i][1], ton2[i][2], ton2[i][3]);
        mp[has2[i]].push_back(i); 
    }
    for (int i = 0; i < n; ++i) a[i] = i;
    sort(a, a + n, cmp);
    dfs(1); mp.clear(); if (!fl) puts("sb"); fl = 0;
}

int main() {
    for (int i = 'a'; i <= 'z'; ++i) ff[i] = i - 'a', f[i - 'a'] = i;
    for (int i = 'A'; i <= 'Z'; ++i) ff[i] = i - 'A' + 26, f[i - 'A' + 26] = i;
    int Tt; scanf("%d", &Tt);
    while (Tt--) work();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3860kb

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

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
Wrong Answer
time: 3ms
memory: 3932kb

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

result:

wrong answer invalid length at case #15