1410282310-hd-seinfeld

Source: Internet
Author: User
 Eclipseinfeld

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1413 accepted submission (s): 697


Problem descriptioni'm out of stories. for years I 've been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. but, alas, not for this one.
You're given a non empty string made in its entirety from opening and closing braces. your task is to find the minimum number of "operations" needed to make the string stable. the definition for being stable is as follows:
1. An empty string is stable.
2. If S is stable, then {s} is also stable.
3. If S and T are both stable, then St (the concatenation of the two) is also stable.
All of these strings are stable :{},{}}, and {{}}}; but none of these :}{,{{}{, nor {}{.
The only operation allowed on the string is to replace an Opening brace with a closing brace, or visa-versa.
 
Inputyour program will be tested on one or more data sets. each data set is described on a single line. the line is a non-empty string of opening and closing braces and nothing else. no string has more than 2000 braces. all sequences are of even length.
The last line of the input is made of one or more '-' (minus signs .)

 
Outputfor each test case, print the following line:
K. N
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one.
Note: There is a blank space before n.
 
Sample Input
}{{}{}{}{{{}---
 
Sample output
1. 22. 03. 1
There are a bunch of parentheses in the question. {} is considered as pairing. {, }},}{ is considered not pairing. For a group of unpaired parentheses, you can pair the brackets by changing the direction of the brackets to calculate the minimum number of changes. The solution logic can be omitted for the case. In this way, the unpaired parentheses can be in the following three cases. {{{ 2 .}}}} 3 .}}}} {of course, it can also be combined into a case of N ...} {... m. For} or {, only one change is required, and for} {, it must be changed twice. This question mainly uses stack knowledge. Code
# Include <stdio. h> # include <string. h> char s [2200], C [2200]; int main () {int len1, len2, len3; int I, j, k = 1; int sum, top; while (scanf ("% s", S) & S [0]! = '-') {Len1 = strlen (s); Top = 1; memset (C, 0, sizeof (c); C [0] = s [0]; for (I = 1; I <len1; I ++) {C [Top] = s [I]; // If (s [I-1] = '{' & S [I] = '}') error: // test data {} If (C [Top-1] = '{' & C [Top] = '}') // whether the stack is connected or not allows the stack group to be compared. Top --; elsetop ++;} len2 = 0; for (I = 0; I <top; I ++) {If (C [I] = '}') len2 + +; else break;} len3 = top-len2;/* If (len1% 2 = 0) sum = len1/2 + len2/2; else sum = len1/2 + len2/2 + 2; in this case, you need to consider the case where the stack is empty */SUM = (len2 + 1) /2 + (len3 + 1)/2; printf ("% d. ", k); k ++; printf (" % d \ n ", sum);} return 0 ;}

 

1410282310-hd-seinfeld

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.