
🔎 문제
Alice needs to send a secret password to Bob. The password consists of Nspace-separated integers. She decides to use a messenger, Eve, to send the password. To ensure that Eve does not steal the password, Alice uses a method of encoding she invented -- by writing it in a loop.
For example, if the password is “37 20 71 33 97”, Alice writes it down as “20 71 33 97 37”. She notifies Bob beforehand that the starting point for the message is the 5th integer, so he knows to decode the message starting from there. To make the password harder to guess, she may also use a different starting point. For example, Alice can also write the password as “71 33 97 37 20” where the starting point is the 4th integer.
Being an experienced hacker, Eve managed to figure out Alice’s encoding scheme (but not the starting points). Furthermore, Eve was Alice’s messenger twice, hence she has two of Alice’s encoded messages. Eve wishes to know whether it is possible that Alice has sent the same secret password twice.
Your task is work out whether it is possible two of those encoded messages are for the same secret password.
입력
Line 1: A single positive integer Ncorresponding to the number of integers in the secret password. (N ≤ 100,000)
Line 2: N space-separated positive integers ai corresponding to the integers in the first encoded message. (ai ≤ 1,500,000,000)
Line 3: N space-separated positive integers bi corresponding to the integers in the second encoded message. (bi ≤ 1,500,000,000)
출력
YES or NO indicating whether it is possible that both loops are for the same password.
예제 입력 1
5
20 71 33 97 37
71 33 97 37 20
예제 출력 1
YES
예제 입력 2
5
20 71 33 97 37
71 33 20 97 37
예제 출력 2
NO
예제 입력 3
6
1 1 2 2 3 3
1 3 3 2 2 1
예제 출력 3
NO
앨리스는 밥에게 비밀 비밀번호를 보내야 합니다. 비밀번호는 공백으로 구분된 정수로 구성됩니다. 앨리스는 메신저인 이브를 사용하여 비밀번호를 전송하기로 결정합니다. 이브가 비밀번호를 훔치지 못하도록 하기 위해 앨리스는 자신이 개발한 인코딩 방법, 즉 반복해서 쓰는 방법을 사용합니다.
예를 들어 비밀번호가 “37 20 71 33 97”인 경우 앨리스는 이를 “20 71 33 97 37”로 적습니다. 앨리스는 밥에게 메시지의 시작점이 5번째 정수임을 미리 알려주므로 밥은 거기서부터 메시지를 해독할 수 있습니다. 비밀번호를 추측하기 어렵게 만들기 위해 앨리스는 다른 시작점을 사용할 수도 있습니다. 예를 들어, 앨리스는 시작점이 4번째 정수인 “71 33 97 37 20”으로 비밀번호를 작성할 수도 있습니다.
숙련된 해커인 이브는 앨리스의 인코딩 체계를 알아냈지만 시작점은 알아내지 못했습니다. 게다가 이브는 앨리스의 메신저를 두 번이나 했기 때문에 앨리스의 인코딩된 메시지 두 개를 가지고 있습니다. 이브는 앨리스가 동일한 비밀 암호를 두 번 전송했을 가능성이 있는지 알고 싶어합니다.
여러분의 임무는 인코딩된 메시지 중 두 개가 동일한 비밀 암호에 대한 것일 가능성이 있는지 알아내는 것입니다.
입력 줄 1: 하나의 양의 정수 N비밀 비밀번호의 정수 수에 해당합니다. (N ≤ 100,000)
2번째 줄 첫 번째 인코딩된 메시지의 정수에 해당하는 공백으로 구분된 양의 정수 ai. (ai ≤ 1,500,000,000)
3번째 줄 두 번째 인코딩된 메시지의 정수에 해당하는 공백으로 구분된 양의 정수 bi. (bi ≤ 1,500,000,000)
출력
두 루프가 동일한 암호에 대한 것일 가능성이 있는지 여부를 나타내는 YES 또는 NO를 출력합니다.
💡 문제 풀이
def is_same_password(a, b):
# 두 메시지의 길이가 다르면 동일한 비밀번호가 아님
if len(a) != len(b):
return "NO"
# 하나의 메시지가 다른 메시지의 부분 순열이면 YES 반환
if ' '.join(map(str, b)) in ' '.join(map(str, a * 2)):
return "YES"
return "NO"
# 입력 읽기
n = int(input())
message_a = list(map(int, input().split()))
message_b = list(map(int, input().split()))
# 동일한 비밀번호인지 확인하고 결과 출력
print(is_same_password(message_a, message_b))
👍🏻 숏코딩
input()
s1 = input().strip()
s2 = input()
s1 = s1 + ' ' + s1
if s2 in s1:
print("YES")
else:
print("NO")'Python > 코딩 테스트' 카테고리의 다른 글
| [백준] 1931번: 회의실 배정 (0) | 2024.11.19 |
|---|---|
| [백준] 14171번: Cities and States (4) | 2024.11.13 |
| [백준] 23905번 Countdown (3) | 2024.11.11 |
| [동적계획법] N으로 표현 (0) | 2024.11.08 |
| [백준] 15829번: Hashing (1) | 2024.11.07 |
댓글