QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#363743#7705. Make Your Own Morse Code Palindromepipoika#AC ✓14ms10348kbPython32.7kb2024-03-24 02:39:032024-03-24 02:39:04

Judging History

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

  • [2024-03-24 02:39:04]
  • 评测
  • 测评结果:AC
  • 用时:14ms
  • 内存:10348kb
  • [2024-03-24 02:39:03]
  • 提交

answer

from collections import defaultdict, deque

def isP(s):
    return "".join(reversed(s)) == s
def outsideM(f, s):
    m = min(map(len, [f, s]))
    # return 
    # print(f, "$", s)
    # print(f[:len(s)], "$",  (''.join(reversed(s))))
    return f[:m] == ''.join(reversed(s))[:m]
def insideM(f, s):
    return outsideM(s, f)

morseRaw = {
    "A": ".-",
    "B": "-...",
    "C": "-.-.",
    "D": "-..",
    "E": ".",
    "F": "..-.",
    "G": "--.",
    "H": "....",
    "I": "..",
    "J": ".---",
    "K": "-.-",
    "L": ".-..",
    "M": "--",
    "N": "-.",
    "O": "---",
    "P": ".--.",
    "Q": "--.-",
    "R": ".-.",
    "S": "...",
    "T": "-",
    "U": "..-",
    "V": "...-",
    "W": ".--",
    "X": "-..-",
    "Y": "-.--",
    "Z": "--..",
    "0": "-----",
    "1": ".----",
    "2": "..---",
    "3": "...--",
    "4": "....-",
    "5": ".....",
    "6": "-....",
    "7": "--...",
    "8": "---..",
    "9": "----.",
}
moseRev = {v: k for k,v in morseRaw.items()}
morseSym =  set(morseRaw.values())
morse = defaultdict(lambda: "", morseRaw)

def getMorse(s):
    return "".join([morse[c] for c in s])

word = input()
p = getMorse(word)
# print(word, p)

if isP(p):
    print(0)
    quit()

def shortest(m):
    r = len(m) + 1
    dp = [500] * r
    short = [None] * r
    dp[0] = 0
    short[0] = ""

    for i in range(1, r):
        for b in range(1, min(5, i)+1):
            seg = m[(i-b):i]
            # print(r, dp)
            if seg in morseSym:
                # print("seg", seg, i-b, i)
                if dp[i-b] + 1 < dp[i]:
                    dp[i] = dp[i-b] + 1
                    short[i] = short[i-b] + moseRev[seg]
                    # print('seg', seg, i, dp[i], dp[i-b])
    
    # print(m, short[-1])
    # print(short)
    return short[-1]

sa = None
for d in range((len(p))//2, len(p)+1):
    if insideM(p[:d], p[d:]) and len(p[:d]) >= len(p[d:]): 
        rev = p[:2*d-len(p)][::-1]
        gen = shortest(rev)
        if not sa or len(gen) < len(sa):
            # print(gen)
            sa = gen
    if insideM(p[:d], p[d+1:]):
        rev = p[:2*d-len(p)+1][::-1]
        # print(d, rev)
        gen = shortest(rev)
        if not sa or len(gen) < len(sa):
            # print(gen)
            sa = gen

print(len(sa), sa)

"""

a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
q = deque()

q.append("")
while q:
    n = q.popleft()
    # print(n, q)
    for c in a:
        suff = c + n
        m = getMorse(suff)
        if not outsideM(p, m):
            continue
        # print("Valid outside")
        if isP(p + m):
            print(len(suff), suff)
            quit()
        q.append(suff)
"""

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 10ms
memory: 10236kb

input:

FOOT

output:

1 L

result:

ok correct

Test #2:

score: 0
Accepted
time: 3ms
memory: 10284kb

input:

FOOTS

output:

3 0QI

result:

ok correct

Test #3:

score: 0
Accepted
time: 7ms
memory: 10336kb

input:

FOOTS

output:

3 0QI

result:

ok correct

Test #4:

score: 0
Accepted
time: 8ms
memory: 10192kb

input:

FOOTSTOOL

output:

0

result:

ok correct

Test #5:

score: 0
Accepted
time: 12ms
memory: 10348kb

input:

OOTNX

output:

2 J0

result:

ok correct

Test #6:

score: 0
Accepted
time: 6ms
memory: 10240kb

input:

3 FRENCH HENS

output:

6 5RCLXB

result:

ok correct

Test #7:

score: 0
Accepted
time: 12ms
memory: 10216kb

input:

TWAS THE NIGHT BEFORE XMAS

output:

13 YXFOL46PF4VPT

result:

ok correct

Test #8:

score: 0
Accepted
time: 13ms
memory: 10216kb

input:

1A2B3C4D5E6F7G8H9I10J

output:

18 0695OW3L4535Y62X1E

result:

ok correct

Test #9:

score: 0
Accepted
time: 12ms
memory: 10212kb

input:

A PARTRIDGE IN A PEAR TREE

output:

8 QF3FFCXC

result:

ok correct

Test #10:

score: 0
Accepted
time: 7ms
memory: 10336kb

input:

TEN LORDS ALEAPING

output:

9 BQ4L4R8XA

result:

ok correct

Test #11:

score: 0
Accepted
time: 8ms
memory: 10244kb

input:

XABCDQRST1234567890123456789

output:

7 6CZCBQU

result:

ok correct

Test #12:

score: 0
Accepted
time: 9ms
memory: 10156kb

input:

ASDFGHJKLQWERTYUIOPMNBVCXZ987

output:

23 ZO12FY5ROP8XYLQPRY73LFF

result:

ok correct

Test #13:

score: 0
Accepted
time: 13ms
memory: 10156kb

input:

THE QUICK BROWN FOX JUMPS OVER

output:

17 24YXQ2C2JR3RCLY5T

result:

ok correct

Test #14:

score: 0
Accepted
time: 13ms
memory: 10248kb

input:

1234567891 IS A PRIME NUMBER 0

output:

18 L37YVUC59123456789

result:

ok correct

Test #15:

score: 0
Accepted
time: 13ms
memory: 10276kb

input:

INTRANSIGENCE

output:

0

result:

ok correct

Test #16:

score: 0
Accepted
time: 13ms
memory: 10336kb

input:

ANTICKING ANTICKING WRECKING A

output:

5 CRCXP

result:

ok correct

Test #17:

score: 0
Accepted
time: 14ms
memory: 10280kb

input:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

output:

1 E

result:

ok correct

Test #18:

score: 0
Accepted
time: 13ms
memory: 10276kb

input:

ISO9001 "IEEEEEEEEEEEEEEEEEEEE

output:

6 90018S

result:

ok correct