acm-Bracket Matching problem

Source: Internet
Author: User

ACM has been admired for a long time, but today only began. Well, the second problem I met was me. (The truth is the first one)

To get to the point, copy the topic below:

  

now, with a sequence of parentheses, you should check that the line brackets are paired.
Input
the first line enters a number n (0<n<=100), which indicates that there are n sets of test data. The next n lines enter multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data groups is less than 5 groups. Data guarantee S contains only "[", "]", "(", ")" Four characters
Output
The
output of each set of input data is one row, if the parentheses contained in the string are paired, the output is yes, and if you do not pair the output no
Sample input
3[(]) (]) ([[[] ()])
Sample output
Nonoyes

When you get to the topic, the most thought of course is the bracket matching question, and did not think about the other. Here's my thought process:
Write an example of a long brace match to find the pattern. As follows:
[([()])()]

Ecstatic, found the following pattern:
1. If the length of the string is odd, it is not possible to match the string. So the strlen () function is used.
2. For an even-numbered condition, start looking for the first character. Find the first one, then find the second one:
If the first and second mismatches indicate that the last bracket is definitely a match, the match is started. (etc.).

When in-depth analysis, found that, well, the model is too idealistic, no. There are serious errors that will only be satisfied if the brackets are fully axisymmetric in the center, PASS.

So, I thought about the other, finally, considering the idea of recursion + elimination, thought of a great idea:
1. Recursion---resolves to the minimum solution;
2. I'm done with the match.
This is always the case in parentheses matching: () or []. When this part is eliminated, the rest of the section will always appear () or [].
So, is it possible to eliminate this as a unit until the end of the entire sequence is eliminated? I think it should be possible.
But how do you implement this part? In what way to achieve, but also a headache problem.
This reminds me of playing music when the "Rhythm Bar" (Let's call it), can be large, can be small, and finally can not.
So, I need two pieces.
1. String header address pointer.
2. Move the pointer.
Always feel a little bit less what, how to express the deletion of the character? Can it be represented in the original string? is a problem again. Need to solve, so think of other solutions:
Starting with the first bit in the string sequence, one judgement, using the "FILO" way to represent, so there are the following ideas:
1. Set the element to a stack, and if the element that is going into the stack matches the top element of the stack, the top element of the stack pops up.
2. Traverse the entire string, and when the traversal is over, determine the current stack bottom and where the stack tops are. If equal, the description matches, otherwise.
Why can't we introduce the error-judging mechanism in the middle?
We can divide these matching elements into two categories:
The class is left and the class is right . (Isn't that nonsense?) )
However, it detects that the elements and top elements of the stack are not in the same class and do not match . Then, we have the right to say: Do not check, this string killed me, I will say no.

If the stack operation is esoteric, you can use an outer array (that is, an array) as a cache buff to delete elements.

So, here's the following:
1. Determine the length of the current string.
2. Copy a string from the first character to the buff.
3. Error-Judging mechanism, if it is in a different class, and matches, then initialize the value at the current index and the dynamic index value of the array element-1.
4. In the meantime, there is no error, and the current string is processed, otherwise no.
5.ok, no problem, return yes.

Above, only solves the string match problem, and also has the output rendering.
All right!!! Output rendering is a number of groups, can be processed in real time?
Ideas:
The result of comparing each row has a result buffer, and finally loops through the output.
20150910, bad code.








  




















acm-Bracket Matching problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.