RobloxOA #14
Cyclic Shift Pairs
估计中等可在线判题
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].
输入格式
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 one integer: the number of cyclic pairs.
样例 1
输入:
9
13 5604 31 2 13 4560 546 654 456
输出:
5
样例 2
输入:
4
10 1 100 10
输出:
1
样例 3
输入:
3
123 231 312
输出:
3
样例说明
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.