QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#669035 | #7882. Linguistics Puzzle | Nightsky | WA | 3ms | 3884kb | C++14 | 2.4kb | 2024-10-23 17:05:12 | 2024-10-23 17:05:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 55;
int n;
int cnt[MAXN][3],tar[MAXN][3],res[MAXN],c[MAXN*MAXN],t[MAXN*MAXN],ans[MAXN];
char S[MAXN*MAXN][5];
bool vis[MAXN];
int trans(char x)
{
if('a'<=x&&x<='z') return x-'a';
else return x-'A'+26;
}
char Itrans(int x)
{
if(x<=26) return 'a'+x;
else return 'A'+x;
}
bool check()
{
for(int i=0;i<n*n;++i) c[i]=0;
for(int i=1;i<=n*n;++i)
{
int len=strlen(S[i]+1);
int tmp=0;
for(int j=1;j<=len;++j) tmp=tmp*n+res[trans(S[i][j])];
if(len>=2&&res[trans(S[i][1])]==0) return false;
c[tmp]++;
}
for(int i=0;i<n*n;++i) if(t[i]!=c[i]) return false;
return true;
}
bool dfs(int p)
{
if(p==n)
{
// cout<<"thisyes"<<endl;
if(check()) return true;
else return false;
}
for(int i=0;i<n;++i)
{
if(vis[i]) continue;
if(cnt[i][0]!=tar[p][0]||cnt[i][1]!=tar[p][1]||cnt[i][2]!=tar[p][2]) continue;
ans[p]=i;
res[i]=p;
vis[i]=1;
if(dfs(p+1)) return true;
vis[i]=0;
}
return false;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;++i) vis[i]=0;
for(int i=0;i<n;++i)
for(int j=0;j<=2;++j) cnt[i][j]=tar[i][j]=0;
for(int i=0;i<n*n;++i) c[i]=t[i]=0;
for(int i=1;i<=n*n;++i)
{
scanf("%s",S[i]+1);
int len=strlen(S[i]+1);
if(len>=2)
{
cnt[trans(S[i][1])][0]++;
cnt[trans(S[i][2])][1]++;
if(S[i][1]==S[i][2]) cnt[trans(S[i][1])][2]++;
}
else cnt[trans(S[i][1])][1]++;
}
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
int tmp=i*j;
t[tmp]++;
if(tmp>=n)
{
tar[tmp/n][0]++;
tar[tmp%n][1]++;
if(tmp/n==tmp%n) tar[tmp%n][2]++;
}
else tar[tmp][1]++;
}
}
// cout<<cnt[0
// if(dfs(0)) cout<<"yes"<<endl;
// else cout<<"no"<<endl;
dfs(0);
for(int i=0;i<n;++i) putchar(Itrans(ans[i]));
printf("\n");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3728kb
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: 3884kb
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: 3828kb
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:
wrong answer invalid character { at case #25