Given two length-N sequences, decide whether one is a cyclic rotation of the other.
Medium5String matchingArrayTwo pointersStringInterviewNo attempts yetTime limit1sMemory limit64 MBAlice has to send a secret password to Bob. The password is N integers separated by spaces. She hands it to a messenger, Eve. To keep Eve from stealing it, Alice hides the password with a scheme she invented: she arranges the integers in a ring and writes them down for one full turn, starting from a position of her choice.
For example, if the password is 37 20 71 33 97, Alice writes 20 71 33 97 37. She told Bob in advance that the starting point is the 5th integer, so Bob reads from there and recovers the password. Alice can write the message from a different starting point. The same password written as 71 33 97 37 20 has the 4th integer as its starting point.
Eve is an experienced hacker and worked out Alice's scheme, but she does not know the starting points. Eve carried Alice's message twice, so she holds two encoded messages. Eve wants to know whether Alice could have sent the same password both times.
Decide whether the two encoded messages can come from the same password.
The first line contains the number of integers in the password, N. (1≤N≤100000)
The second line contains the integers of the first encoded message, a1,a2,…,aN, separated by spaces. (1≤ai≤1500000000)
The third line contains the integers of the second encoded message, b1,b2,…,bN, separated by spaces. (1≤bi≤1500000000)
Print YES on the first line if the two encoded messages can come from the same password, and NO otherwise.