ZCO Matched Brackets 2

Could someone please hack my solution for Matched Brackets 2? I am unable to figure out a case for which it is giving WA although it fails on many tasks on the system. :frowning:

https://www.codechef.com/viewsolution/8783833

Your program fails on test cases like :

10

1 1 2 3 4 1 2 3 4 2 -----> corresponding sequence ( ( ) [ ] ( ) [ ] )

Your output:

1 10 2

Expected output

2 10 2

Although It’s very hard for me to judge the correction, I can comment that your method of finding alternating depth has some flaw.

Another thing I’d like to say is that try to use a stack. It will be a lot more easier to debug.

See this implementation here.

Thanks a lot Viresh!

Still gives WA though https://www.codechef.com/viewsolution/8786653 I’ll work on it.

Please help me to find the flaw in my logic.

https://www.codechef.com/viewsolution/8787047

If possible please give some test case where it fails.

Thanks in advance.

@likecs

I’d again like to give you the same advice … Try it using a stack. Debugging will be a lot more easier. For Now you can use the following test case to see the fault :

20

3 1 3 1 3 1 3 1 2 4 1 2 3 4 2 4 2 4 2 4
// sequence -> [ ( [ ( [ ( [ ( ) ] ( ) [ ] ) ] ) ] ) ]

Your Output : 9 18 20
The correct answer is : 8 18 20

Again the algorithm for alternating depth might have few flaws. Reconsider the increment in the answer that you are doing.

You’re welcome Sandy. And here’s another case to help you :

20

1 1 3 1 2 3 4 4 1 2 3 1 3 1 2 4 2 4 2 2

( ( [ ( ) [ ] ] ( ) [ ( [ ( ) ] ) ] ) )

Your output - 7 20 8

Correct answer - 5 20 8

@virresh
Please help me too with some Test Case
https://www.codechef.com/viewsolution/9995916

The Solutions are not visible right now. Please add another link where I can see your code.

Here is my solution. https://www.codechef.com/viewsolution/11729767
I cannot find any wrong test case.
The test case mentioned above are also correct.
When I submit the last 2 test case of both sub task are correct rest are wrong.
Can someone give me a test case for which my code will give wrong output? @virresh

Excuse me again. Ever for karma problems I cannot open my own post. Since it is similar question, I post my solution for INOI 2015 Brackets. here
I organized I tree where to nest the brackets, but it continue to give me mistakes (of cours the routine print tree get deleted when submit).
The only reason that explain this, is that I cannot get the logic.
Took a sequence like this:

  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • ( ) ( [ ] { } ) ] ( { } ] [ )

for my code all the following sequences are valid :

  • 12 ()
  • 1238 ()(…)
  • 1 2 3 4 5 8 ()([…])
  • 1 2 3 4 5 6 7 8 ()([]{})
  • 3 8 (…) + all the combinations of 3 8 and nested sequences
  • 4 5 [] + all the nested squence taked alone
  • 10 15 (…)
  • 11 12 {}
  • 10 11 12 15 ({} …)

but are not valid for example:

  • 3 8 10 15 (…) (…) because there is a] between them.

did I do something wrong?

I forgot also to say: took a sequence like this (() I just take as valid 23 and reject 13 (it overwrites the stack)

A testcase provided earlier by @virresh

10
1 1 2 3 4 1 2 3 4 2 -----> corresponding sequence ( ( ) [ ] ( ) [ ] )

Expected Output is : 2 10 2

How is the alternating depth 2? Could someone help me out here…

How is the alternating depth 2?