Oh wait, I forgot that ZCOPRAC is a contest.
Why dont they enable viewing other’s solution for ZCOPRAC, INOIPRAC and IARCSJUD? That would be really helpful.
Since this is an unusually subtle one, I’ll give you a hint: the error is in sortbysecdesc. There’s something you’re forgetting to do. The result might vary according to which version of gcc you use.
You’re supposed to print the earliest occurrence of the largest nesting depth/ largest gap between matched brackets, but there’s nothing in his comparator that ensures this.
Interestingly, despite the fact that the contents of depths and symbolsare ordered by position before the call to std::sort, it seems that this ordering is destroyed by the call to sort in some circumstances, because std::sort is not stable.
The effect of this is that, in some unusual circumstances, callling sort will order by decreasing nesting depth/ gap between matches (as expected), but, amongst entries with the same nesting depth/ gap, the original ordering in terms of position is lost.
There are two ways to fix this: either fix up the sortbysecdesc, or, I would speculate, to use std::stable_sort instead of sort.
As I said: unusually subtle, and I had to resort to generating loads of random testcases to find one that broke it - trying to contrive a situation that showed the instability of std::sort wasn’t getting me anywhere (and might not expose the error across different versions of gcc’s standard library)