Editorial for Dãy ngoặc đúng

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.


Submitting an official solution before solving the problem yourself is a bannable offence.

Sử dụng ngăn xếp chứa dãy ngoặc mở, lần lượt duyệt từ trái sang phải:

  • Nếu gặp ngoặc mở thì đưa vào ngăn xếp.
  • Nếu gặp ngoặc đóng thì xét phần tử tại đỉnh ngăn xếp:
    • Nếu là ngoặc mở cùng loại thì xóa phần tử tại đỉnh ngăn xếp.
    • Nếu là ngoặc mở khác loại thì xóa phần tử tại đỉnh ngăn xếp, đồng thời tăng ~ans~ lên 1.
    • Nếu ngăn xếp rỗng thì kết quả là -1.
  • Kết quả là ~ans~

Comments

Please read the guidelines before commenting.


There are no comments at the moment.