"Nine Degrees" topic 1337: Looking for the longest valid bracket sequence __ Bracket Matching series

Source: Internet
Author: User
Topic Address: http://ac.jobdu.com/problem.php?pid=1337
Topic Description:
Give you a sequence of parentheses with a length of N, consisting of ' (' and ') ', you can find the longest valid bracket sequence in this sequence. The meaning of the valid bracket sequence is that in this sequence, all the opening parentheses have a unique closing parenthesis match; all the closing brackets have a unique left parenthesis match. For example: ((())) () () is a valid bracket sequence with a length of 10, and (())) (not. What you need to solve is to find the length of the longest valid bracket subsequence, and find the number of sequences with such lengths. Input:The test data includes multiple, each test data contains two rows: the first behavior an integer n, where n does not exceed 10^6. The second behavior is a string of length n, which is composed of the left parenthesis ' (' and Right parenthesis ') '. Output:For each test case, the output line, which contains two integers, represents the length and number of the longest valid bracket sequence, separated by a space in the middle. Returns 0 1 if no legal subsequence exists. Sample Input:
6
(()) ()
3
)) (
Sample output:
6 1
0 1

Basic idea and topic 1153: parentheses match the same problem.
There is an input array with a bit array of flags.
When the left and right brackets are found to match, the current position is 1.
The maximum value of the time, the circular sign bit, found 1 consecutive, on the accumulation, otherwise set to 0.
At the same time need to compare size, cumulative results.

C + + AC

#include <stdio.h> #include <stack> #include <string> #include <string.h> using namespace std;
const int MAXN = 1000002;
Char INPUT[MAXN];
    int main () {int n,i;
        while (scanf ("%d", &n)!= EOF) {scanf ("%s", input);
        Stack<int> Symstack;
        int *flagarr = new Int[n];
        memset (flagarr,0,sizeof (Flagarr));
            for (i = 0; i < n; i++) {if (input[i] = = ' (') {Symstack.push (i);
                }else if (input[i] = = ') ' &&!symstack.empty ()) {int j = symstack.top ();
                Symstack.pop ();
                Flagarr[i] = 1;
            FLAGARR[J] = 1;
        an int maxlen = 0;
        int count = 0;
        int templen = 0;
            for (i = 0; i < n; i++) {if (flagarr[i] = = 1) {Templen + +;
            }else {templen = 0; } if (Templen > MaxLen) {maxlen = temPLen; 
            Count = 1;
            }else if (Templen = = maxlen) {count++;
        } if (MaxLen = 0) {printf ("0 1\n");
        }else{printf ("%d%d\n", maxlen,count);
} return 0;  }/************************************************************** problem:1337 user:wangzhenqing Language: C + + result:accepted time:100 ms memory:10092 KB ************************************************************ ****/

Java AC

Import Java.util.Scanner;
  
Import Java.util.Stack; public class Main {* * * 1337/public static void Main (string[] args) {Scanner Scanner = new
        Scanner (system.in);
            while (Scanner.hasnext ()) {int n = scanner.nextint ();
            String a = Scanner.next ();
            Char array[] = A.tochararray ();
            int flagarr[] = new Int[n];
            stack<integer> stack = new stack<integer> ();
                for (int i = 0; i < n; i++) {if (array[i] = = ' (') {Stack.push (i);
                    }else if (array[i] = = ') ' &&!stack.isempty ()) {int j = Stack.pop ();
                    Flagarr[i] = 1;
                FLAGARR[J] = 1;
            an int maxlen = 0;
            int count = 0;
            int templen = 0;
  for (int i = 0; i < n; i++) {if (flagarr[i] = = 1) {Templen + +;              }else {templen = 0;
                    } if (Templen > MaxLen) {maxlen = Templen; 
                Count = 1;
                }else if (Templen = = maxlen) {count++; } System.out.println (maxlen = 0?)
        "0 1": maxlen+ "" +count); }}}/************************************************************** problem:1337 user:wzqwsrf Langua Ge:java result:accepted time:2130 Ms memory:80436 KB ******************************************************* *********/

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.