QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#273959 | #7882. Linguistics Puzzle | ucup-team992# | RE | 16ms | 9240kb | Python3 | 1.3kb | 2023-12-03 06:54:09 | 2023-12-03 06:54:09 |
Judging History
answer
from collections import defaultdict
import itertools
for t in range(int(input())):
N = int(input())
S = input().split()
buckets = defaultdict(list)
cf = [0]*N
cs = [0]*N
for i in range(N):
for j in range(N):
hi = (i*j)//N
lo = (i*j)%N
cf[hi] += 1
cs[lo] += 1
for i in range(N):
buckets[(cf[i], cs[i])].append(i)
z = None
for s in S:
if S.count(s) == 2*N-1:
z = s
break
cfs = defaultdict(int)
css = defaultdict(int)
sbux = defaultdict(list)
S2 = []
for s in S:
if len(s) == 1:
s = z + s
S2.append(s)
cfs[s[0]] += 1
css[s[1]] += 1
S2 = sorted(S2)
for k in css:
sbux[(cfs[k], css[k])].append(k)
#print(sbux, buckets)
for k in sbux:
assert len(sbux[k]) == len(buckets[k])
S = sorted(S)
dfs_map = {}
def dfs(order, i):
if i == len(order):
SC = []
for x in range(N):
for y in range(N):
hi = (x*y)//N
lo = (x*y)%N
SC.append(dfs_map[hi] + dfs_map[lo])
if sorted(SC) == S2:
return True
for p in itertools.permutations(sbux[order[i]]):
for j in range(len(p)):
dfs_map[buckets[order[i]][j]] = p[j]
if dfs(order, i+1):
return True
dfs(sorted(sbux.keys()), 0)
out = [None] * N
for idx in dfs_map:
out[idx] = dfs_map[idx]
print("".join(out))
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 9240kb
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: 16ms
memory: 9140kb
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
Dangerous Syscalls
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