Submission details
Task:Swapping letters
Sender:MrAurela
Submission time:2020-09-19 14:36:04 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#40.37 sdetails
#50.36 sdetails
#6ACCEPTED0.04 sdetails
#70.38 sdetails
#80.38 sdetails
#90.38 sdetails
#100.35 sdetails

Code

import queue

n = int(input())
forbidden = set()
for i in range(n):
    tup = input().split()
    forbidden.add(tup[0]+tup[1])
    forbidden.add(tup[1]+tup[0])
x = input()
y = input()

left = queue.Queue()
left.put(x)
visited = set()

while not left.empty():
    current = left.get()
    #print(current)
    if current == y:
        break
    if current not in visited:
        for i in range(len(current)-1):
            if current[i]+current[i+1] not in forbidden:
                nex = current[:i]+current[i+1]+current[i]+current[i+2:]
                left.put(nex)
        visited.add(current)

if current == y:
    print("YES")
else:
    print("NO")

Test details

Test 1

Verdict: ACCEPTED

input
5
a b
b c
c d
d e
...

correct output
YES

user output
YES

Test 2

Verdict: ACCEPTED

input
2
a b
b c
acbbaca
cabbaac

correct output
YES

user output
YES

Test 3

Verdict: ACCEPTED

input
2
a b
b c
acbbaca
baccaab

correct output
NO

user output
NO

Test 4

Verdict:

input
10
d c
e b
f y
h q
...

correct output
YES

user output
(empty)

Test 5

Verdict:

input
10
a i
a l
d a
g h
...

correct output
NO

user output
(empty)

Test 6

Verdict: ACCEPTED

input
325
a b
a e
a f
a g
...

correct output
YES

user output
YES

Test 7

Verdict:

input
325
a c
a e
a g
a h
...

correct output
NO

user output
(empty)

Test 8

Verdict:

input
0
dlkinfmdyjaofxbccwhhbxzartqwdr...

correct output
YES

user output
(empty)

Test 9

Verdict:

input
0
bxisdrdpgcsnnvhnfgimivzqpqjwqc...

correct output
NO

user output
(empty)

Test 10

Verdict:

input
0
mrwduerojcguvxzmbomfsainvqehsl...

correct output
NO

user output
(empty)