DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Cipher Guess

Back to problem

Editorial

Check whether each word is a candidate, then count only candidate words. Consider one test case, and let LLL be the maximum length among SSS and all ciphertext words.

Since the ciphertext words appear before SSS in the input, store the words first and then read SSS. A word CCC with a different length from SSS cannot be a candidate. Otherwise, initialize two arrays of size 262626. The first records the ciphertext letter assigned to each plaintext letter, and the second records the plaintext letter assigned to each ciphertext letter.

Process the letters x,yx,yx,y at each corresponding position. Reject CCC if xxx was already assigned to a different letter or if yyy was already assigned from a different letter. Otherwise, record the correspondence in both arrays. If no contradiction occurs, CCC is a candidate. Reset both arrays before checking the next word, since the substitutions are independent.

Use an ordered map whose keys are the actual candidate strings to count their occurrences. If there are no candidates, output -1. Otherwise, choose a key with the largest frequency, breaking ties by choosing the lexicographically smallest key. Different strings must be counted separately even if they have the same substitution pattern.

Checking candidates takes O(NL)O(NL)O(NL) time. A string comparison takes O(L)O(L)O(L) time, so the total time complexity with an ordered map is O(NLlog⁡(N+1))O(NL\log(N+1))O(NLlog(N+1)). The space complexity is .

Proof

Suppose a valid substitution turns SSS into CCC. Their lengths must be equal. Equal plaintext letters have equal ciphertext letters, so the first array cannot encounter a contradiction. Different plaintext letters have different ciphertext letters, so the second array cannot encounter a contradiction either. Therefore, every candidate passes the check.

Conversely, suppose CCC passes the check. Every letter occurring in SSS is assigned to exactly one ciphertext letter, and different plaintext letters are assigned to different ciphertext letters. Thus, the recorded assignments are a one-to-one correspondence between the letters used in the two words. The numbers of unassigned plaintext letters and unused ciphertext letters are equal. Pair them arbitrarily to extend the recorded assignments to a valid substitution on all letters. This substitution turns into exactly , so every word passing the check is a candidate.

In subtask 1, every word has length 111, so every ciphertext word is a candidate. Count the occurrences of each letter in an array of size 262626 and select the answer in O(N)O(N)O(N) time and O(1)O(1)O(1) space.

In subtask 2, candidate checking can compare every pair of positions p,qp,qp in equal-length words and verify that and have the same truth value. For each candidate, directly compare it with every input word to count its occurrences. This takes time and space to store the words.

O(NL)O(NL)O(NL)
2626
26
SSS
CCC

Each input word is processed once, and its string's count is incremented exactly when it is a candidate. The map therefore stores the exact frequency of each candidate. Selecting the highest frequency and then the lexicographically smallest tied word gives precisely the required answer.

,
q
Sp=SqS_p=S_qSp​=Sq​
Cp=CqC_p=C_qCp​=Cq​
O(NL2+N2L)O(NL^2+N^2L)O(NL2+N2L)
O(NL)O(NL)O(NL)

Solution written by GPT6