QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606136#7882. Linguistics Puzzlequannguyen2009WA 5ms3796kbC++232.0kb2024-10-02 22:23:242024-10-02 22:23:26

Judging History

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

  • [2024-10-02 22:23:26]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3796kb
  • [2024-10-02 22:23:24]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;

const int N=65, mod = 1e9+7, inf = 1e18;

int n;
string s[N*N];
int cn1[N], cn2[N], cl1[N], cl2[N], idx[305], res[N];
char ch[N];
map<ii,vector<int>> mp, mp2;
vector<int> num;
vector<ii> pr;
bool b = 0;

bool ok() {
    vector<int> tmp;
    for(int i=0; i<n*n; i++) {
        int val = 0;
        for(char c: s[i]) val=val*n+res[idx[c]];
        tmp.pb(val);
    }
    sort(all(tmp));
    return tmp==num;
}

void backtrack(int pos) {
    if(pos==sz(pr)){
        if(ok()) b=1;
        return;
    }
    vector<int> v=mp[pr[pos]], v2=mp2[pr[pos]];
    sort(all(v2));
    do {
        for(int i=0; i<sz(v); i++) res[v2[i]]=v[i];
        backtrack(pos+1);
        if(b) return;
    } while(next_permutation(all(v2)));
}

void solve() {
    cin >> n;
    num.clear(); pr.clear(); mp.clear(); mp2.clear();
    for (int i=0; i<N; i++) res[i] = 0;
    for(int i=0; i<n; i++) cn1[i]=cn2[i]=cl1[i]=cl2[i]=0;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            int x=i*j;
            if(x/n) cn1[x/n]++;
            cn2[x%n]++;
            num.pb(x);
        }
    }
    sort(all(num));
    for(int i=0; i<n*n; i++) {
        cin >> s[i];
        cl2[idx[s[i].back()]]++;
        if((int)s[i].size()>=2) cl1[idx[s[i][0]]]++;
    }

    for(int i=0;i<n;i++){
        mp[{cn1[i], cn2[i]}].pb(i);
        mp2[{cl1[i], cl2[i]}].pb(i);
        pr.pb({cn1[i], cn2[i]});
    }
    sort(all(pr)); pr.erase(unique(all(pr)), pr.end());   
    backtrack(0);
    string ans(n, '0');
    for(int i=0; i<n; i++) ans[res[i]]=ch[i];
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    for(int i=0; i<26; i++) idx['a'+i] = i, ch[i] = 'a'+i;
    for(int i=0; i<26; i++) idx['A'+i] = 26+i, ch[26+i]='A'+i;
    int t; cin >> t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: 5ms
memory: 3796kb

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
prisdombkjqghfencla
tcrdpoaklmjihfgeqsbn
utiraponmlksghjfecdbq
qotsrvjunmlkpiegfhdcba
pvutsrhwoimlkjnqgfedbca
xbvuts...

result:

wrong answer The product 2*12=24 is not in the output at case #17