c++stringalgorithmtreeinterval-tree

Update and check if the substring containing brackets is correct


The problem is:

Time limit: 0.2s

In these cases, a correct brackets expression is defined:

My main idea is similar to the problem 380C on CodeForces, http://codeforces.com/blog/entry/10363

Then I check if the longest subsequence in the given ranges is equal to the length of ranges, so I will get the answer. But I got time limit error.

I have been searching for this on the internet all day but I haven't got the answer. I will be grateful if you help me. :)

Here is my code: https://github.com/hoangvanthien/GH_CppFiles/blob/master/SPOJ/NKBRACKE.cpp


Solution

  • The conditions for a valid bracket sequence are:

    So, from the original string of open and close brackets, we can convert it into sequence of number, each number represent the difference between open and close brackets from the beginning of the sequence. Each open brackets, we will plus one, and minus one for close.

    For example, for ((())))) -> we have the sequence { 1, 2 , 3, 2 , 1, 0, -1, -2 }

    So, to test if a substring is valid, for example, substring (2, 5), which is ())), we need to see if at any point, the difference between open and close is negative. From above sequence, we have {3, 2, 1, 0}, and we need to minus 2 for each element, as we need to remove those brackets from the beginning of the string, which are not in the substring. -> we have {1, 0, -1, -2} -> so the substring is invalid.

    After understand above idea, we can have our solution for the problem.

    And from all of that requirements, we can use a Segment tree, which give you O(log n) update and O(log n) retrieve.

    Pseudo code

    SegmentTree tree;
    
    Initialize the tree with original sequence
    
    for each query in the tree
       if( query type is update)
          if(change from ) to ()
             increase all value by 2 from range index to n
          else if(change from ( to ) )
             decrease all value by 2 from range index to n
       else
          int min = tree.getMinimumValueInRange(u, v)
          int notInSubstring = tree.getMinimumValueInRange(u - 1, u - 1)
          if(min - notInSubstring < 0)
              print Invalid 
          else if( length of substring is not even)
              print Invalid
          else if( tree.getMinimumValueInRange(v, v) != notInSubstring)//Number of open and close brackets are not equaled.
              print Invalid