SDUT 1451 brackets

Source: Internet
Author: User

The Time Limit: 1000 MS Memory limit: 65536K topic description is known to provide several strings that only contain "(" or! The length of the string is less than or equal to 100! Calculate the maximum number of valid substrings and the number of valid substrings !! The format of valid strings must be as follows: 1. "()" 2. "()" 3. "()" is invalid, for example: 1. ") (" 2. "()" 3. "() (" 4 ....... the input contains several groups of test data, each of which is a string. For each group of test data, the maximum valid substrings and the number of valid substrings are output (if the maximum valid substrings are 0, the output is 0 1 )! Sample input) () sample output 0 16 2 prompt source Azheng @ SDJZU sample program this is a question of our weekly competition, it seems that it is almost the simplest. This question cannot be clearly stated, especially when the number of valid strings is finally obtained, this is the error of this question, in fact, this question asks for the maximum number of valid strings. At the beginning, I calculated the number of valid strings, but it was WA again and again. Finally, I asked someone else, only then can we know that the question is based on the maximum number of valid strings. In fact, finding the number of all valid strings in this question is also a good question. Finding the maximum number of valid strings makes this question a little simpler. [Cpp] # include <stdio. h> # include <string. h> # include <math. h> char s1 [1000000]; char statck [1000000]; struct num {int pos; char c;} B [1000000]; int main () {int I, j, n, m, s, t, l, top, k, res, max; while (scanf ("% s", s1 )! = EOF) {l = strlen (s1); top = 0; for (I = 0; I <L-1; I ++) {if (s1 [I] = '(') {B [top]. pos = I; B [top ++]. c = '(';} else if (s1 [I] = ') {if (top> 0 & B [top-1]. c = '(') {top-= 1 ;}else {top = 0 ;}}for (I = top-1; I >=0; I --) {s1 [B [I]. pos] = ')';} top = 0; max = 0; res = 0; k = 0; for (I = 0, s = 0; I <= L-1; I ++) {if (s1 [I] = '(') {statck [top ++] = '(';} else if (s1 [I] = ') {if (top> 0 & statck [top-1] =' (') {s + = 2; top-= 1; k = 1;} else {if (max <s & K = 1) {max = s; s = 0; res = 1;} else if (max = s & k = 1) {res ++; s = 0;} s = 0; top = 0; k = 0 ;}} if (k = 1) {if (max <s) {max = s; res = 1 ;}else if (max = s) {res ++ ;}} if (max! = 0) {www.2cto.com printf ("% d \ n", max, res);} else {printf ("0 1 \ n") ;}} return 0 ;}

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.