RobloxOA #14
Cyclic Shift Pairs
Estimated MediumJudge-ready
A cyclic shift moves some trailing digits of a number (in decimal) to the front, with the remaining digits shifted back in their original order. For two integers a and b with the same number of digits, if a can become b through a cyclic shift (by 0 or more digits), a and b form a cyclic pair.
Given an array a of positive integers, count the index pairs (i,j) with 0≤i<j<n such that a[i] and a[j] have the same number of digits and a[i] equals some cyclic shift of a[j].
Input
The first line contains n; the second line contains n positive integers a[0..n−1]. 1≤n≤100000, 1≤a[i]≤10^9.
Output
Output one integer: the number of cyclic pairs.
Example 1
Input:
9
13 5604 31 2 13 4560 546 654 456
Output:
5
Example 2
Input:
4
10 1 100 10
Output:
1
Example 3
Input:
3
123 231 312
Output:
3
Example notes
Example 1: a=[13,5604,31,2,13,4560,546,654,456]; the cyclic pairs are (0,2), (0,4), (2,4) (13 and 31), (1,5) (5604 and 4560), and (6,7) (546 and 654), 5 pairs in total. 546 can only pair with 546, 465, 654, so it does not form a cyclic pair with 456; 4560 and 456 have different numbers of digits. Example 2: in [10,1,100,10], only the two 10s form a pair. Example 3: 123, 231, 312 are pairwise cyclic shifts of each other, 3 pairs in total.
Editorial
Loading solution…