50195. Second Largest Distance

I'm a slow walker, but I never walk backwards.

Task Description

Write a program to find the second-longest distance between the same digits for 1, 2, and 3, respectively. The input has a sequence of 1, 2, and 3's. The distance between the closest pair of the same digits is the number of other digits between them, plus one. There will be at least two different distances for 1, 2, and 3, respectively, so the second-longest distances for all of them are well-defined. Your program will output the positions of the pair of digits with the second-largest distance. Suppose more than one pair of the same digit has the same second-largest distance, output the pair that appears latest in input.

Input Format

The input is a line of 1, 2, and 3's. You should process all of them until EOF.

Output Format

There are three lines in the output. The first line has the second-largest distance between a pair of 1's and their positions (starting from 0) in the input. The second and the third line are for 2 and 3.

Sample Input 1

1 2 1 3 2 2 3 3 1 2 2 2 3

Sample Output 1

2 0 2
3 1 4
3 3 6

Sample Input 2

In this sample, all the possible distance and pair of positions in digit 3 are as follow,

(3, 1, 4), (2, 4, 6), (1, 6, 7), (3, 7, 10), (2, 10, 12)

As mentioned above, the second-largest distance and the postions should be (2, 10, 12).

2 3 2 2 3 1 3 3 2 2 3 1 3 1

Sample Output 2

2 11 13
2 0 2
2 10 12

Estimated Cyclomatic Number

13.82

Discussion