#P10476. Necklace Matching
Necklace Matching
Necklace Matching
Yuan found a priceless gemstone necklace but forgot its owner! Many people sent emails claiming the necklace belonged to them. However, at most one person is telling the truth. Each claimant wrote a description of the necklace. The necklace is represented by a sequence of digits from 0 to 9. A valid representation is obtained by reading the digits in order starting from any gemstone and moving clockwise around the necklace. For example, the necklace below:
has the following four possible representations:
$0123$, $1230$, $2301$, $3012$
Your task is to write a program that, given two necklace descriptions, determines whether they represent the same necklace. Note that the necklace is not flipped; only cyclic rotations (clockwise) are allowed.
Formally, let the two descriptions be strings A and B. They represent the same necklace if and only if
\( \exists i, \ 0 \le i < n \) such that \( B = A[i]\;A[i+1]\;\ldots A[n-1]A[0]\;A[1]\;\ldots A[i-1] \), where \( n \) is the length of the strings.
inputFormat
The input consists of two lines. Each line contains a non-empty string of digits (from 0 to 9) representing a necklace.
outputFormat
Output a single line containing Yes
if the two strings represent the same necklace, or No
otherwise.
sample
0123
1230
Yes